-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathopenmptarget.cpp
63 lines (57 loc) · 1.89 KB
/
openmptarget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "data.h"
#include <vector>
#include <chrono>
#include <iostream>
#include <omp.h>
namespace chrono = std::chrono;
int main()
{
printf("OpenMP target.\n");
int elems = data.extent(0);
printf("Procesing %d * %d elements\n", elems, elems);
int* divergence = new int[elems * elems];
auto indexeddivergence = stdex::basic_mdspan<int, stdex::extents<stdex::dynamic_extent, stdex::dynamic_extent>>(divergence, elems, elems);
auto computestart = chrono::steady_clock::now();
uint8_t* origdata = data.data();
#pragma omp target teams map(to:origdata[:elems*side*side]) map(from:divergence[:elems*elems])
{
mnisttype data = mnisttype(origdata, elems);
auto indexeddivergence = stdex::basic_mdspan<int, stdex::extents<stdex::dynamic_extent, stdex::dynamic_extent>>(divergence, elems, elems);
#pragma omp distribute parallel for
for (int i = 0; i < elems; i++)
{
#pragma omp parallel for
for (int j = 0; j < elems; j++)
{
int sum = 0;
for (int y = 0; y < side; y++)
{
for (int x = 0; x < side; x++)
{
int diff = (int) data(i, y, x) - (int) data(j, y, x);
sum += diff * diff;
}
}
indexeddivergence(i, j) = sum;
}
}
}
auto firstend = chrono::steady_clock::now();
int maxi = 0;
int maxj = 0;
for (int i = 0; i < elems; i++)
{
for (int j = 0; j < elems; j++)
{
if (indexeddivergence(i, j) > indexeddivergence(maxi, maxj))
{
maxi = i;
maxj = j;
}
}
}
auto secondend = chrono::steady_clock::now();
std::cout << "First pass, in microseconds : " << chrono::duration_cast<chrono::microseconds>(firstend-computestart).count() << std::endl;
std::cout << "Second pass, in microseconds : " << chrono::duration_cast<chrono::microseconds>(secondend-firstend).count() << std::endl;
printf("Maximum divergence at %d against %d with value %d\n", maxi, maxj, indexeddivergence(maxi, maxj));
}