-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcudafy.el
321 lines (269 loc) · 9.06 KB
/
cudafy.el
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
(defun cudafy/for2if()
"example of calling a external command.
passing text of region to its stdin.
replace region by its stdout."
(interactive)
(let ((cmd
(format
"./auxiliary/cudafy-stdin.py for2if"
)))
(shell-command-on-region (region-beginning) (region-end) cmd nil "REPLACE" nil t)))
(defun cudafy/kji-irregular()
"example of calling a external command.
passing text of region to its stdin.
replace region by its stdout."
(interactive)
(let ((cmd
(format
"./auxiliary/cudafy-stdin.py kji_irregular"
)))
(shell-command-on-region (region-beginning) (region-end) cmd nil "REPLACE" nil t)))
(defun cudafy/kji-general-offset()
"example of calling a external command.
passing text of region to its stdin.
replace region by its stdout."
(interactive)
(let ((cmd
(format
"./auxiliary/cudafy-stdin.py kji_general_offset"
)))
(shell-command-on-region (region-beginning) (region-end) cmd nil "REPLACE" nil t)))
(defun cudafy/find-global-variable()
"find the global variable used in the kernel,
put them in the kernel parameter list"
(interactive)
(let (
(cmd (format "./auxiliary/cudafy-stdin.py find_variable"))
(cuda-lvalue nil)
(cudafy-var nil))
(shell-command-on-region (region-beginning) (region-end) cmd "*cudafy*" nil nil t)
(with-current-buffer "*cudafy*"
(goto-char (point-min))
(setq cuda-lvalue
(buffer-substring-no-properties
(line-beginning-position)
(line-end-position)))
(next-line)
(setq cudafy-var
(buffer-substring-no-properties
(line-beginning-position)
(line-end-position)))
)
(kill-new cudafy-var)
(kill-new cuda-lvalue)
;; (switch-to-buffer-other-window "*cudafy*")
(view-buffer-other-window "*cudafy*")
(other-window 1)
(goto-char (region-beginning))
(search-backward ")")
(if (y-or-n-p "add global variable in the function?")
(insert cudafy-var)
nil)
(delete-other-windows)
))
(defun cudafy/find-cpu-arrs()
"find the global array used in the kernel and put them in the kernel parameter list"
(interactive)
(let (
(cmd (format "./auxiliary/cudafy-c2g-stdin.py find_arrs_in_func"))
(cudafy-array nil))
(shell-command-on-region (region-beginning) (region-end) cmd "*cudafy*" nil nil t)
(with-current-buffer "*cudafy*"
(goto-char (point-min))
(setq cudafy-array
(buffer-string))
)
(kill-new cudafy-array)
))
(defun cudafy/find-array()
"find the global array used in the kernel and put them in the kernel parameter list"
(interactive)
(let (
(cmd (format "./auxiliary/cudafy-stdin.py find_array"))
(cudafy-array nil))
(shell-command-on-region (region-beginning) (region-end) cmd "*cudafy*" nil nil t)
(with-current-buffer "*cudafy*"
(goto-char (point-min))
(setq cudafy-array
(buffer-string))
)
(kill-new cudafy-array)
;; (switch-to-buffer-other-window "*cudafy*")
(view-buffer-other-window "*cudafy*")
(other-window 1)
(goto-char (region-beginning))
(search-backward ")")
(if (y-or-n-p "add global array in the function?")
(yank)
nil)
(delete-other-windows)
))
(defun cudafy/constuct-kernel()
"add kernel name and width, height, depth to a for loop"
(interactive)
(let ((cuda-thread-index-string
" int width = blockIdx.x * blockDim.x + threadIdx.x;
int height = blockIdx.y * blockDim.y + threadIdx.y;
int depth = blockIdx.z * blockDim.z + threadIdx.z;\n")
(start (min (region-beginning) (region-end)))
(end (max (region-beginning) (region-end)))
(fn-name nil))
(setq fn-name (read-from-minibuffer "input the kernel name: "))
(goto-char end)
(insert "}\n")
(goto-char start)
(insert (format "__global__ void cuda_%s()\n{\n" fn-name))
(insert cuda-thread-index-string)
))
(defun cudafy/3D-wrapper()
"construct cuda 3D kernel"
(interactive)
(let (
(cmd (format "./auxiliary/cudafy-stdin.py make_3D_wrapper"))
(dim-block nil)
(cudafy-wrapper nil))
(shell-command-on-region (point-min) (point-max) cmd "*cudafy*" nil nil t)
;; copy the *cudafy* buffer
(with-current-buffer "*cudafy*"
(goto-char (point-min))
(setq cudafy-wrapper
(buffer-string))
)
(setq dim-block " dim3 dimBlock(32, 4, 2);
dim3 dimGrid((nx + 2 + 31)/32, (ny + 2 + 3)/4, (nz + 2 + 1)/2);\n\n")
;; (switch-to-buffer-other-window "*cudafy*")
(view-buffer-other-window "*cudafy*")
(other-window 1)
(if (y-or-n-p "add 3D dimBlock variable?")
(insert dim-block)
nil)
(if (y-or-n-p "add wrapper function?")
(insert cudafy-wrapper)
nil)
(delete-other-windows)
))
(defun cudafy/change-to-2D-arg ()
"insert 2D kernel block thread kernels argument"
(interactive)
(let ((type (read-from-minibuffer "input 2D kernel type: 1(xy) 2(xz) 3(yz) :"))
(id1 nil)
(id2 nil)
(dim-block nil))
(if (string= type "1")
(progn
(setq id1 "nx")
(setq id2 "ny")))
(if (string= type "2")
(progn
(setq id1 "nx")
(setq id2 "nz")))
(if (string= type "3")
(progn
(setq id1 "ny")
(setq id2 "nz")))
(setq dim-block
(format " dim3 dimBlock2_%s(32, 4, 1);
dim3 dimGrid2_%s(( %s + 2 + 31)/32, (%s + 2 + 3)/4, 1);\n" type type id1 id2))
(insert dim-block)
(re-search-forward "dimBlock" (line-end-position))
(replace-match (format "dimBlock2_%s" type))
(beginning-of-line)
(re-search-forward "dimGrid" (line-end-position))
(replace-match (format "dimGrid2_%s" type))
(beginning-of-line)
))
(defun cudafy/alloc()
"alloc device memory for arrs"
(interactive)
(let ((cmd
(format
"./auxiliary/cudafy-c2g-stdin.py alloc"
)))
(shell-command-on-region (region-beginning) (region-end) cmd nil "REPLACE" nil t)))
(defun cudafy/dealloc()
"dealloc device memory"
(interactive)
(let ((cmd
(format
"./auxiliary/cudafy-c2g-stdin.py dealloc"
)))
(shell-command-on-region (region-beginning) (region-end) cmd nil "REPLACE" nil t)))
(defun cudafy/upload()
"upload arrs from cpu to gpu"
(interactive)
(let ((cmd
(format
"./auxiliary/cudafy-c2g-stdin.py upload"
)))
(shell-command-on-region (region-beginning) (region-end) cmd nil "REPLACE" nil t)))
(defun cudafy/upload_func()
"upload arrs in a function from cpu to gpu"
(interactive)
(let ((cmd
(format
"./auxiliary/cudafy-c2g-stdin.py upload_func"
)))
(shell-command-on-region (region-beginning) (region-end) cmd "*cudafy*" nil nil t)
(with-current-buffer "*cudafy*"
(kill-new (buffer-string)))
))
(defun cudafy/download()
"download arrs in a function from gpu to cpu"
(interactive)
(let ((cmd
(format
"./auxiliary/cudafy-c2g-stdin.py download"
)))
(shell-command-on-region (region-beginning) (region-end) cmd nil "REPLACE" nil t)
(with-current-buffer "*cudafy*"
(kill-new (buffer-string)))
))
(defun cudafy/download_func()
"download arrs in a function from gpu to cpu"
(interactive)
(let ((cmd
(format
"./auxiliary/cudafy-c2g-stdin.py download_func"
)))
(shell-command-on-region (region-beginning) (region-end) cmd "*cudafy*" nil nil t)))
(defun cudafy/function_transform()
"function name transform"
(interactive)
(let ((cmd
(format
"./auxiliary/cudafy-stdin.py func_name_transform"
)))
(shell-command-on-region (region-beginning) (region-end) cmd nil "REPLACE" nil t)
(with-current-buffer "*cudafy*"
(kill-new (buffer-string)))
))
(defun cudafy/register_cpu_arrs()
"function name transform"
(interactive)
(let ((cmd
(format
"./auxiliary/cudafy-c2g-stdin.py register_cpu_arrs"
)))
(shell-command-on-region (region-beginning) (region-end) cmd "*cudafy*" nil nil t)
(with-current-buffer "*cudafy*"
(kill-new (buffer-string)))
))
(spacemacs/set-leader-keys
"oa" 'cudafy/find-array
"oA" 'cudafy/find-cpu-arrs
"ov" 'cudafy/find-global-variable
"of" 'cudafy/for2if
"oF" 'cudafy/function_transform
"og" 'cudafy/kji-general-offset
"oc" 'cudafy/constuct-kernel
"ow" 'cudafy/3D-wrapper
"oi" 'cudafy/change-to-2D-arg
"or" 'cudafy/kji-irregular
"our" 'cudafy/register_cpu_arrs
"ouU" 'cudafy/upload
"ouu" 'cudafy/upload_func
"ouD" 'cudafy/download
"oud" 'cudafy/download_func
"oua" 'cudafy/alloc
"ouc" 'cudafy/dealloc
)