-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathmain.cpp
More file actions
189 lines (149 loc) · 5.28 KB
/
main.cpp
File metadata and controls
189 lines (149 loc) · 5.28 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
// Copyright (c) 2019-2026 Ben Ashbaugh
//
// SPDX-License-Identifier: MIT
*/
#include <popl/popl.hpp>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb/stb_image_write.h>
#include <CL/opencl.hpp>
#include <chrono>
const char* filename = "julia.bmp";
const float cr = -0.123f;
const float ci = 0.745f;
static const char kernelString[] = R"CLC(
kernel void Julia( global uchar4* dst, float cr, float ci )
{
const float cMinX = -1.5f;
const float cMaxX = 1.5f;
const float cMinY = -1.5f;
const float cMaxY = 1.5f;
const int cWidth = get_global_size(0);
const int cIterations = 16;
int x = (int)get_global_id(0);
int y = (int)get_global_id(1);
float a = x * ( cMaxX - cMinX ) / cWidth + cMinX;
float b = y * ( cMaxY - cMinY ) / cWidth + cMinY;
float result = 0.0f;
const float thresholdSquared = cIterations * cIterations / 64.0f;
for( int i = 0; i < cIterations; i++ ) {
float aa = a * a;
float bb = b * b;
float magnitudeSquared = aa + bb;
if( magnitudeSquared >= thresholdSquared ) {
break;
}
result += 1.0f / cIterations;
b = 2 * a * b + ci;
a = aa - bb + cr;
}
result = max( result, 0.0f );
result = min( result, 1.0f );
// RGBA
float4 color = (float4)( result, sqrt(result), 1.0f, 1.0f );
dst[ y * cWidth + x ] = convert_uchar4(color * 255.0f);
}
)CLC";
int main(
int argc,
char** argv )
{
int platformIndex = 0;
int deviceIndex = 0;
size_t iterations = 16;
size_t gwx = 512;
size_t gwy = 512;
size_t lwx = 0;
size_t lwy = 0;
{
popl::OptionParser op("Supported Options");
op.add<popl::Value<int>>("p", "platform", "Platform Index", platformIndex, &platformIndex);
op.add<popl::Value<int>>("d", "device", "Device Index", deviceIndex, &deviceIndex);
op.add<popl::Value<size_t>>("i", "iterations", "Iterations", iterations, &iterations);
op.add<popl::Value<size_t>>("", "gwx", "Global Work Size X AKA Image Width", gwx, &gwx);
op.add<popl::Value<size_t>>("", "gwy", "Global Work Size Y AKA Image Height", gwy, &gwy);
op.add<popl::Value<size_t>>("", "lwx", "Local Work Size X", lwx, &lwx);
op.add<popl::Value<size_t>>("", "lwy", "Local Work Size Y", lwy, &lwy);
bool printUsage = false;
try {
op.parse(argc, argv);
} catch (std::exception& e) {
fprintf(stderr, "Error: %s\n\n", e.what());
printUsage = true;
}
if (printUsage || !op.unknown_options().empty() || !op.non_option_args().empty()) {
fprintf(stderr,
"Usage: julia [options]\n"
"%s", op.help().c_str());
return -1;
}
}
std::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);
printf("Running on platform: %s\n",
platforms[platformIndex].getInfo<CL_PLATFORM_NAME>().c_str() );
std::vector<cl::Device> devices;
platforms[platformIndex].getDevices(CL_DEVICE_TYPE_ALL, &devices);
printf("Running on device: %s\n",
devices[deviceIndex].getInfo<CL_DEVICE_NAME>().c_str() );
cl::Context context{devices[deviceIndex]};
cl::CommandQueue commandQueue{context, devices[deviceIndex]};
cl::Program program{ context, kernelString };
program.build();
cl::Kernel kernel = cl::Kernel{ program, "Julia" };
cl::Buffer deviceMemDst = cl::Buffer{
context,
CL_MEM_ALLOC_HOST_PTR,
gwx * gwy * sizeof( cl_uchar4 ) };
// execution
{
kernel.setArg(0, deviceMemDst);
kernel.setArg(1, cr);
kernel.setArg(2, ci);
cl::NDRange lws; // NullRange by default.
printf("Executing the kernel %d times\n", (int)iterations);
printf("Global Work Size = ( %d, %d )\n", (int)gwx, (int)gwy);
if( lwx > 0 && lwy > 0 )
{
printf("Local Work Size = ( %d, %d )\n", (int)lwx, (int)lwy);
lws = cl::NDRange{lwx, lwy};
}
else
{
printf("Local work size = NULL\n");
}
// Ensure the queue is empty and no processing is happening
// on the device before starting the timer.
commandQueue.finish();
auto start = std::chrono::system_clock::now();
for( size_t i = 0; i < iterations; i++ )
{
commandQueue.enqueueNDRangeKernel(
kernel,
cl::NullRange,
cl::NDRange{gwx, gwy},
lws);
}
// Ensure all processing is complete before stopping the timer.
commandQueue.finish();
auto end = std::chrono::system_clock::now();
std::chrono::duration<float> elapsed_seconds = end - start;
printf("Finished in %f seconds\n", elapsed_seconds.count());
}
// save bitmap
{
auto buf = reinterpret_cast<const uint32_t*>(
commandQueue.enqueueMapBuffer(
deviceMemDst,
CL_TRUE,
CL_MAP_READ,
0,
gwx * gwy * sizeof(cl_uchar4) ) );
stbi_write_bmp(filename, (int)gwx, (int)gwy, 4, buf);
printf("Wrote image file %s\n", filename);
commandQueue.enqueueUnmapMemObject(
deviceMemDst,
(void*)buf );
}
return 0;
}