-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodeGen.hs
569 lines (498 loc) · 20.7 KB
/
CodeGen.hs
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
{-# LANGUAGE RecordWildCards, QuasiQuotes#-} -- , OverloadedStrings #-}
module CodeGen where
import Compiler
import Data.Char
import System.Process
import System.Directory
import System.FilePath
import Types
import Text.RawString.QQ
import Text.Format
-- Code generation begins here
spaced :: String -> String
spaced t = " " ++ t ++ " "
parens :: String -> String
parens t = "(" ++ t ++ ")"
brackets :: String -> String
brackets t = "[" ++ t ++ "]"
separate :: String -> [String] -> String
separate x ys = foldl1 (\l r -> l ++ x ++ r) ys
kernelBody :: Expr -> String
kernelBody (FuncCall f args) = format temp [
kernelArgs,
body
]
where
temp = [r|
kernel void GPUMonad({0}) {
{1}
}
|]
kernelArgs = case f of
"filter" -> "__global int *out, __global int *count"
"reduce" -> "__global int *gOut, __global int *gCountWhere, __global int *gCountDone, __local int *lOut"
"map" -> "__global int *out"
"plow" -> "__global int *out, __global int *count"
"zipWith" -> "__global int *out"
body = case f of
"filter" -> format temp [
funcName,
listArg
]
where
[TIdf funcName _, TIdf listArg _] = args
temp = [r|
if ({0}({1}[get_global_id(0)])) {
out[atomic_inc(count)] = {1}[get_global_id(0)];
}
|]
"reduce" -> format temp [
funcName,
listArg
]
where
[TIdf funcName _, TIdf listArg _] = args
temp = [r|
lOut[get_local_id(0)] = {1}[get_global_id(0)];
barrier(CLK_LOCAL_MEM_FENCE);
if (!get_local_id(0)) {
for (int i = 1; i < get_local_size(0); ++i) {
lOut[0] = {0}(lOut[0], lOut[i]);
}
gOut[atomic_inc(gCountWhere)] = lOut[0];
barrier(CLK_GLOBAL_MEM_FENCE);
int myTurn = atomic_inc(gCountDone);
if (myTurn == get_num_groups(0) - 1) {
for (int i = 1; i < get_num_groups(0); ++i) {
gOut[0] = {0}(gOut[0], gOut[i]);
}
}
}
|]
"map" -> format temp [
funcName,
listArg
]
where
[TIdf funcName _, TIdf listArg _] = args
temp = [r|
out[get_global_id(0)] = {0}({1}[get_global_id(0)]);
|]
"plow" -> format temp [
funcName,
listArg
]
where
[TIdf funcName _, TIdf listArg _] = args
temp = [r|
out[get_global_id(0)] = {1}[get_global_id(0)];
barrier(CLK_GLOBAL_MEM_FENCE);
if (!get_local_id(0)) {
int offset = get_local_size(0) * get_group_id(0);
for (int i = 1; i < get_local_size(0); ++i) {
out[offset + i] = {0}(out[offset + i - 1], out[offset + i]);
}
int myTurn = atomic_inc(count);
if (myTurn == get_num_groups(0) - 1) {
for (int group = 1; group < get_num_groups(0); ++group) {
offset = group * get_local_size(0);
for (int workItem = 0; workItem < get_local_size(0); ++workItem) {
out[offset + workItem] = {0}(out[offset - 1], out[offset + workItem]);
}
}
}
}
|]
"zipWith" -> format temp [
funcName,
listArg1,
listArg2
]
where
[TIdf funcName _, TIdf listArg1 _, TIdf listArg2 _] = args
temp = [r|
out[get_global_id(0)] = {0}({1}[get_global_id(0)], {2}[get_global_id(0)]);
|]
generateDef :: Expr -> String
generateDef t = case t of
Type (TIdf varName _) (Idf "Int") -> "__constant int " ++ varName ++ " = {!" ++ varName ++ "_contents}"
Type (TIdf varName _) (List (Idf "Int")) -> "__constant int " ++ varName ++ "[{!" ++ varName ++ "_length}] " ++ " = {!" ++ varName ++ "_contents}"
Def (TIdf varName _) (Num num) -> "__constant int " ++ varName ++ " = " ++ show num
Def (TIdf varName _) (TIdf funcName (FuncSig n)) ->
"int "
++ varName
++ (parens argList)
++ " {\n return "
++ funcName ++ "(" ++ callList ++ ");\n"
++ "}"
where
argList = separate ", " (map (\x -> "int v" ++ show x) [1..n])
callList = separate ", " (map (\x -> "v" ++ show x) [1..n])
FuncDef funcName args body -> "int "
++ funcName
++ (parens argList)
++ " {\n return "
++ bodyText
++ ";\n}"
where
argList = separate ", " (map (\x -> "int " ++ x) args)
bodyText = toOpenCLC body
_ -> ""
generateSig :: Expr -> String
generateSig t = case t of
FuncDef funcName args _ -> "int "
++ funcName
++ "("
++ (separate ", " (replicate (length args) "int"))
++ ")"
Def (TIdf funcName (FuncSig n)) _ -> "int "
++ funcName
++ "("
++ (separate ", " (replicate n "int"))
++ ")"
_ -> ""
base :: String
base = [r|
int mod(int a, int b) {
return a % b;
}
|]
toOpenCLC :: Expr -> String
toOpenCLC t = case t of
Let exprs body -> unlines [base, sigs, globals, kernel]
where
sigs = (separate ";\n" (filter (/="") (map generateSig exprs))) ++ ";\n"
globals = (foldl1 (\x y -> x ++ ";\n" ++ y) $ map generateDef exprs)
kernel = kernelBody body
TIdf varName _ -> varName
Num num -> show num
Bln True -> show 1
Bln False -> show 0
Numeric op l r -> (parens $ toOpenCLC l) ++ spaced op ++ (parens $ toOpenCLC r)
Boolean op l r -> (parens $ toOpenCLC l) ++ spaced op ++ (parens $ toOpenCLC r)
Compose _ _ _ -> error "compose not implemented"
IfE c t f -> (parens $ toOpenCLC c) ++ " ? " ++ (parens $ toOpenCLC t) ++ " : " ++ (parens $ toOpenCLC f)
FuncCall funcName args -> funcName ++ (parens $ separate ", " $ map parens $ map toOpenCLC args)
_ -> error $ "Unsupported: " ++ show t
-- Library generation
getInput (Type (TIdf varName varType) _) = [(varName, varType)]
getInput _ = []
getInputs = concat . map getInput
generateCppSignature :: Expr -> String -> String
generateCppSignature (Let exprs body) kernelName = returnType ++ " " ++ kernelName ++ parens argList
where
inputs = getInputs exprs
convertInput (List (Idf "Int")) = "std::vector<int>"
convertInput (Idf "Int") = "int"
convertedInputs = map (\(varName, varType) -> (varName, convertInput varType)) inputs
argList = separate ", " parts
where
parts = map (\(varName, varType) -> varType ++ " " ++ varName) convertedInputs
FuncCall primitive _ = body
returnType = case primitive of
"filter" -> "std::vector<int>"
"reduce" -> "int"
"map" -> "std::vector<int>"
"plow" -> "std::vector<int>"
"zipWith" -> "std::vector<int>"
generateKernelReplacementCode :: Expr -> String
generateKernelReplacementCode (Let exprs body) = unlines $ map makeReplaceCode inputs
where
inputs = getInputs exprs
makeReplaceCode (varName, Idf "Int") = format temp [varName]
where
temp = [r|
kernelSource = replace(kernelSource, "{!{0}_contents}", std::to_string({0}));
|]
makeReplaceCode (varName, List (Idf "Int")) = format temp [varName]
where
temp = [r|
auto {0}_strings = map([](int i){return std::to_string(i);}, {0});
kernelSource = replace(kernelSource, "{!{0}_length}", std::to_string({0}.size()));
kernelSource = replace(kernelSource, "{!{0}_contents}", braces(params({0}_strings)));
|]
-- TODO: The actual purpose of the function is unclear. What does n do in the
-- generated programs? This needs to be looked at.
generateWorkerSizeDiscoveryCode :: Expr -> String
generateWorkerSizeDiscoveryCode (Let exprs body) = case primitive of
"filter" ->
"int n = " ++ arg ++ ".size();"
++ mod64Check "n"
where
FuncCall _ [_, TIdf arg _] = body
"reduce" ->
"int n = " ++ arg ++ ".size();\n"
++ mod64Check (arg ++ ".size()")
where
FuncCall _ [_, TIdf arg _] = body
"map" ->
"int n = " ++ arg ++ ".size();\n"
++ mod64Check "n"
where
FuncCall _ [_, TIdf arg _] = body
"plow" ->
"int n = " ++ arg ++ ".size();\n"
++ mod64Check "n"
where
FuncCall _ [_, TIdf arg _] = body
"zipWith" ->
"int n = std::min(" ++ l ++ ".size(), " ++ r ++ ".size());\n"
++ mod64Check "n"
where
FuncCall _ [_, TIdf l _, TIdf r _] = body
where
FuncCall primitive _ = body
mod64Check count = format temp [count]
where
temp = [r|
if (({0}) % workGroupSize != 0) {
throw std::invalid_argument("Job size should be multiple of" + std::to_string(workGroupSize));
}
|]
-- Name, Flags (read/write), size (integers)
data Buffer = Buffer String String String
-- Or just size (in integers)
| Local String
deriving (Show)
generateBuffers :: Expr -> [Buffer]
generateBuffers (Let exprs body) = case primitive of
"filter" -> [
Buffer "out" "CL_MEM_WRITE_ONLY" "n",
Buffer "count" "CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR" "1"
]
"reduce" -> [
Buffer "gOut" "CL_MEM_WRITE_ONLY" "xs.size()/workGroupSize",
Buffer "gCountWhere" "CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR" "1",
Buffer "gCountDone" "CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR" "1",
Local "workGroupSize"
]
"map" -> [
Buffer "out" "CL_MEM_WRITE_ONLY" "n"
]
"plow" -> [
Buffer "out" "CL_MEM_READ_WRITE" "n",
Buffer "count" "CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR" "1"
]
"zipWith" -> [
Buffer "out" "CL_MEM_WRITE_ONLY" "n"
]
where
FuncCall primitive _ = body
generateOutputArray :: Expr -> String
generateOutputArray (Let exprs body) = case primitive of
"filter" -> withCountVar
"plow" -> withCountVar
"reduce" -> [r|
auto h_out = std::make_unique<int[]>(n / workGroupSize);
std::fill_n(h_out.get(), n / workGroupSize, 0);
auto h_count = std::make_unique<int[]>(1);
h_count[0] = 0;
|]
_ -> [r|
auto h_out = std::make_unique<int[]>(n);
std::fill_n(h_out.get(), n, 0);
|]
where
FuncCall primitive _ = body
withCountVar = [r|
auto h_out = std::make_unique<int[]>(n);
std::fill_n(h_out.get(), n, 0);
auto h_count = std::make_unique<int[]>(1);
h_count[0] = 0;
|]
generateKernelLiteral :: Expr -> String
generateKernelLiteral program = format ([r|
std::string kernelSource = R"Fancy0penCL({0})Fancy0penCL";
|]) [toOpenCLC program]
generateExtractionCode :: Expr -> String
generateExtractionCode (Let exprs body) = case primitive of
"filter" -> fewInts
"reduce" -> singleInt
_ -> allInts
where
FuncCall primitive _ = body
singleInt = "queue.enqueueReadBuffer(d_gOut, CL_TRUE, 0, sizeof(int), h_out.get());"
allInts = "queue.enqueueReadBuffer(d_out, CL_TRUE, 0, n * sizeof(int), h_out.get());"
fewInts = [r|
queue.enqueueReadBuffer(d_out, CL_TRUE, 0, n * sizeof(int), h_out.get());
queue.enqueueReadBuffer(d_count, CL_TRUE, 0, sizeof(int), h_count.get());
|]
generateReturnCode :: Expr -> String
generateReturnCode (Let exprs body) = case primitive of
"filter" -> fewInts
"reduce" -> singleInt
_ -> allInts
where
FuncCall primitive _ = body
singleInt = "return h_out[0];"
allInts = "return std::vector<int>(h_out.get(), h_out.get() + n);"
fewInts = "return std::vector<int>(h_out.get(), h_out.get() + h_count[0]);"
deviceDiscoveryCode = [r|
// Query platforms
std::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);
if (platforms.size() == 0) {
std::cout << "Platform size 0\n";
throw std::domain_error("Platform size is zero, cannot run.");
}
// Get list of devices on default platform and create context
cl_context_properties properties[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)(platforms[0])(), 0};
cl::Context context(CL_DEVICE_TYPE_GPU, properties);
std::vector<cl::Device> devices = context.getInfo<CL_CONTEXT_DEVICES>();
// Create command queue for first device
cl::CommandQueue queue(context, devices[0], 0, &err);
|]
programBuildCode = [r|
//Build kernel from source string
cl::Program::Sources source(1, std::make_pair(kernelSource.c_str(),kernelSource.size()));
cl::Program program = cl::Program(context, source);
std::cout << program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(devices[0]);
try {
program.build(devices);
} catch (cl::Error& e) {
if (e.err() == CL_BUILD_PROGRAM_FAILURE) {
// Check the build status
cl_build_status status = program.getBuildInfo<CL_PROGRAM_BUILD_STATUS>(devices[0]);
// Get the build log
std::string name = devices[0].getInfo<CL_DEVICE_NAME>();
std::string buildlog = program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(devices[0]);
std::cout << "Build log for " << name << ":" << "\n" << buildlog << "\n";
std::cout << "Program options: " << program.getBuildInfo<CL_PROGRAM_BUILD_OPTIONS>(devices[0]) << "\n";
} else {
throw e;
}
}
|]
kernelRunCode = [r|
// Number of work items in each local work group
cl::NDRange localSize(workGroupSize);
// Number of total work items - localSize must be devisor
cl::NDRange globalSize((int) (ceil(n / (float)workGroupSize) * workGroupSize));
// Enqueue kernel
cl::Event event;
queue.enqueueNDRangeKernel(
kernel,
cl::NullRange,
globalSize,
localSize,
NULL,
&event);
// Block until kernel completion
event.wait();
|]
errorPrintCode = [r|
std::cerr << "ERROR: "<<err.what()<<"("<<err.err()<<")"<< "\n";
return {};
|]
-- Generates the C++ source file containing the function
generateLibrary :: Expr -> String -> String
generateLibrary program name = unlines [
-- Enables exceptions in OpenCL API
"#define __CL_ENABLE_EXCEPTIONS\n"
-- Includes for writing to stdout in case of error
, "#include <iostream>"
-- For std::unique_ptr
, "#include <memory>"
-- For std::ceil() and other math functions
, "#include <cmath>"
-- Here we include the header file we generate
, "#include \"" ++ name ++ ".hpp\""
-- Contains general purpose functions like string replacement
, "#include \"utils.hpp\""
-- Include the OpenCL API
, "#include \"CL/cl.hpp\""
-- Generate the CPP signature
, generateCppSignature program name ++ " {"
-- Pick our favourite workgroup size. At the moment this is hardcoded,
-- however this magic number is specific to graphics cards. Can impact
-- performance.
, "int workGroupSize = 64;"
-- Generates the actual OpenCL program
, generateKernelLiteral program
-- Generates code that specializes the OpenCL program for the arguments
, generateKernelReplacementCode program
-- Generates code that checks how much workers are needed based on the
-- input arguments. For example, when filtering you want the same
-- amount as workers as the amount of elements. But when zipWith'ing
-- you only want the amount of workers equal to the shortest of the two
-- lists. Additionally, this amount should be divisible by workgroupsize.
, generateWorkerSizeDiscoveryCode program
-- Generate code that creates buffers
, unlines (map generateBufferDefinition buffers)
-- Creates an array in which the result of the kernel will be stored
, generateOutputArray program
, "cl_int err = CL_SUCCESS;"
, "try {"
-- Code that picks the first usable device for kernel execution
, deviceDiscoveryCode
-- Initialize the created buffers
, unlines (map generateBufferInitialization buffers)
-- Insert code that compiles the program;
, programBuildCode
-- Create a kernel object from the program, and tell it which
-- function to execute. OpenCL programs generated by Hywar
-- are always called GPUMonad
, "cl::Kernel kernel(program, \"GPUMonad\", &err);"
-- Generate code that sets the arguments of the kernel
, unlines (zipWith generateKernelBufferArgSet [0..length buffers - 1] buffers)
-- Insert code that runs the kernel
, kernelRunCode
-- Extract the results
, generateExtractionCode program
-- Error handling
, "} catch (cl::Error err) {"
, errorPrintCode
, "}"
-- Code that returns the result in the proper container (int or std::vector<int>)
, generateReturnCode program
, "}"
]
where
-- Finds out how many buffers are needed
buffers = generateBuffers program
-- Generates a buffer definition if it is needed
-- Local buffers do not need to be defined and initialized.
generateBufferDefinition (Buffer name _ _) = "cl::Buffer d_" ++ name ++ ";\n"
generateBufferDefinition (Local _) = ""
-- Count buffers need to be initialized to zero for proper counting
-- Generates buffer initialization code.
generateBufferInitialization (Buffer "gCountWhere" flags size) = format [r|
d_gCountWhere = cl::Buffer(context, {0}, ({1}) * sizeof(int), h_count.get());
|] [flags, size]
generateBufferInitialization (Buffer "gCountDone" flags size) = format [r|
d_gCountDone = cl::Buffer(context, {0}, ({1}) * sizeof(int), h_count.get());
|] [flags, size]
generateBufferInitialization (Buffer "count" flags size) = format [r|
d_count = cl::Buffer(context, {0} , ({1}) * sizeof(int), h_count.get());
|] [flags, size]
generateBufferInitialization (Buffer name flags size) =
"d_" ++ name ++ " = cl::Buffer(context, " ++ flags ++ ", (" ++ size ++ ") * sizeof(int));"
generateBufferInitialization (Local _) = ""
generateKernelBufferArgSet i (Buffer name _ size) = "kernel.setArg(" ++ show i ++ ", d_" ++ name ++ ");"
generateKernelBufferArgSet i (Local size) = "kernel.setArg(" ++ show i ++ ", (" ++ size ++ ") * sizeof(int), nullptr);"
-- Generates the C++ header file containing the function declaration
generateHeader :: Expr -> String -> String
generateHeader program name = unlines [
"#ifndef " ++ (map toUpper name) ++ "_H",
"#define " ++ (map toUpper name) ++ "_H",
"#include <vector>",
generateCppSignature program name ++ ";",
"#endif"
]
where
-- Generates source and header files and saves them in the directory named the given name
generateAndSaveLibrary :: Expr -> String -> IO()
generateAndSaveLibrary program name = do
createDirectoryIfMissing False name
copyFile "utils.cpp" (name ++ [pathSeparator] ++ "utils.cpp")
copyFile "utils.hpp" (name ++ [pathSeparator] ++ "utils.hpp")
writeFile (name ++ [pathSeparator] ++ name ++ ".cpp") $ generateLibrary program name
writeFile (name ++ [pathSeparator] ++ name ++ ".hpp") $ generateHeader program name
-- Generates all example programs (6003-6007)
generateAllExamples = do
sequence_ $ map (\x -> generateAndSaveLibrary (doTaal x) ("test" ++ show x)) [6003..6007]
-- Executes the test
doTest = do
generateAllExamples
callCommand "make -j4"
callCommand "./kernel_test"