-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTests.cpp
More file actions
137 lines (107 loc) · 3.53 KB
/
Tests.cpp
File metadata and controls
137 lines (107 loc) · 3.53 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "CommandLineParser.h"
#include "ComputePlatform.h"
#include "LinearAlgebraOperation.h"
#include "FillBufferOperation.h"
#include <iostream>
#include <vector>
#include <omp.h>
using namespace std;
int main(int Argc, char* ArgV[])
{
try
{
CommandLineParser CmdParser{ Argc, ArgV };
int PlatformID = 0;
if (CmdParser.OptionExists("--platform"))
{
auto PlatformIDStr = CmdParser.GetOptionValue("--platform");
PlatformID = std::atoi(PlatformIDStr.data());
}
int TestToPerform = 0;
if (CmdParser.OptionExists("--test"))
{
auto TestIDStr = CmdParser.GetOptionValue("--test");
TestToPerform = std::atoi(TestIDStr.data());
}
ComputePlatform TheComputePlatform(PlatformID);
cout << TheComputePlatform << endl;
if (TestToPerform == 0)
{
auto Queue = TheComputePlatform.GetNextCommandQueue();
int N = 10;
int BufferSize = N * sizeof(float);
cl::Buffer aBuffer(TheComputePlatform.Context, CL_MEM_READ_WRITE, BufferSize);
const float One = 1.0f;
cl::Event FillBufferEvent;
Queue.enqueueFillBuffer(aBuffer, One, 0, BufferSize, nullptr, &FillBufferEvent);
FillBufferEvent.wait();
vector<float> BufferContents(N);
Queue.enqueueReadBuffer(aBuffer, CL_TRUE, 0, BufferSize, BufferContents.data());
for (auto i : BufferContents)
{
cout << i << " ";
}
cout << endl;
}
if (TestToPerform == 1)
{
auto Queue = TheComputePlatform.GetNextCommandQueue();
cl::Buffer aBuffer(TheComputePlatform.Context, CL_MEM_READ_WRITE, sizeof(int));
int Value;
Queue.enqueueReadBuffer(aBuffer, CL_TRUE, 0, sizeof(int), &Value);
cout << Value << endl;
}
if (TestToPerform == 2)
{
int NumDevices = static_cast<int>(TheComputePlatform.Devices.size());
int NumThreads = 2;
LinearAlgebraOperation LinAlg{ TheComputePlatform };
FillBufferOperation FillBuffer{ TheComputePlatform };
int N = 10000;
const int GridSize = 30;
int MatrixSize = N * N * sizeof(double);
int VectorSize = N * sizeof(double);
vector<double> ResultVector(GridSize * GridSize);
# pragma omp parallel num_threads(NumThreads)
{
int ThreadId = omp_get_thread_num();
# pragma omp critical
cout << "Thread ID: " << ThreadId << endl;
//auto Queue = TheComputePlatform.GetNextCommandQueue();
int DeviceID = ThreadId % NumDevices;
cl::CommandQueue Queue(TheComputePlatform.Context, TheComputePlatform.Devices[DeviceID], CL_QUEUE_PROFILING_ENABLE);
cl::Buffer MatrixBuffer(TheComputePlatform.Context, CL_MEM_READ_WRITE, MatrixSize);
cl::Buffer VectorBuffer(TheComputePlatform.Context, CL_MEM_READ_WRITE, VectorSize);
cl::Buffer ResultBuffer(TheComputePlatform.Context, CL_MEM_READ_WRITE, VectorSize);
cl::Buffer CacheBuffer(TheComputePlatform.Context, CL_MEM_READ_WRITE, VectorSize);
FillBuffer.FillDoubleBuffer(Queue, MatrixBuffer, 1.0, N * N).wait();
FillBuffer.FillDoubleBuffer(Queue, VectorBuffer, 1.0, N).wait();
# pragma omp for
for (int i = 0; i < GridSize; ++i)
{
# pragma omp critical
cout << i << " " << flush;
for (int j = 0; j < GridSize; ++j)
{
LinAlg.MatVecMul(Queue, MatrixBuffer, VectorBuffer, ResultBuffer, N).wait();
double DotResult = LinAlg.DotProduct(Queue, VectorBuffer, ResultBuffer, N, CacheBuffer);
ResultVector[i + j * GridSize] = DotResult;
}
}
}
}
}
catch (cl::Error& err)
{
cout << "CL ERROR: " << err.what() << " " << err.err() << endl;
}
catch (runtime_error& e)
{
cout << "ERROR: " << e.what() << endl;
}
catch (...)
{
cout << "Unknown Error" << endl;
}
return 0;
}