Mercurial > hg > graal-jvmci-8
comparison src/gpu/ptx/vm/gpu_ptx.cpp @ 11485:49bb1bc983c6
Implement several missing PTX codegen features; return value capture and method args passing of java method executed on GPU.
author | bharadwaj |
---|---|
date | Fri, 30 Aug 2013 16:39:05 -0400 |
parents | d876002b98e6 |
children | c99e65785936 |
comparison
equal
deleted
inserted
replaced
11484:2aac62d79af4 | 11485:49bb1bc983c6 |
---|---|
27 #include "runtime/gpu.hpp" | 27 #include "runtime/gpu.hpp" |
28 #include "utilities/globalDefinitions.hpp" | 28 #include "utilities/globalDefinitions.hpp" |
29 #include "utilities/ostream.hpp" | 29 #include "utilities/ostream.hpp" |
30 #include "memory/allocation.hpp" | 30 #include "memory/allocation.hpp" |
31 #include "memory/allocation.inline.hpp" | 31 #include "memory/allocation.inline.hpp" |
32 #include "kernelArguments.hpp" | |
32 | 33 |
33 void * gpu::Ptx::_device_context; | 34 void * gpu::Ptx::_device_context; |
35 int gpu::Ptx::_cu_device = 0; | |
34 | 36 |
35 gpu::Ptx::cuda_cu_init_func_t gpu::Ptx::_cuda_cu_init; | 37 gpu::Ptx::cuda_cu_init_func_t gpu::Ptx::_cuda_cu_init; |
36 gpu::Ptx::cuda_cu_ctx_create_func_t gpu::Ptx::_cuda_cu_ctx_create; | 38 gpu::Ptx::cuda_cu_ctx_create_func_t gpu::Ptx::_cuda_cu_ctx_create; |
37 gpu::Ptx::cuda_cu_ctx_detach_func_t gpu::Ptx::_cuda_cu_ctx_detach; | 39 gpu::Ptx::cuda_cu_ctx_destroy_func_t gpu::Ptx::_cuda_cu_ctx_destroy; |
38 gpu::Ptx::cuda_cu_ctx_synchronize_func_t gpu::Ptx::_cuda_cu_ctx_synchronize; | 40 gpu::Ptx::cuda_cu_ctx_synchronize_func_t gpu::Ptx::_cuda_cu_ctx_synchronize; |
39 gpu::Ptx::cuda_cu_device_get_count_func_t gpu::Ptx::_cuda_cu_device_get_count; | 41 gpu::Ptx::cuda_cu_device_get_count_func_t gpu::Ptx::_cuda_cu_device_get_count; |
40 gpu::Ptx::cuda_cu_device_get_name_func_t gpu::Ptx::_cuda_cu_device_get_name; | 42 gpu::Ptx::cuda_cu_device_get_name_func_t gpu::Ptx::_cuda_cu_device_get_name; |
41 gpu::Ptx::cuda_cu_device_get_func_t gpu::Ptx::_cuda_cu_device_get; | 43 gpu::Ptx::cuda_cu_device_get_func_t gpu::Ptx::_cuda_cu_device_get; |
42 gpu::Ptx::cuda_cu_device_compute_capability_func_t gpu::Ptx::_cuda_cu_device_compute_capability; | 44 gpu::Ptx::cuda_cu_device_compute_capability_func_t gpu::Ptx::_cuda_cu_device_compute_capability; |
43 gpu::Ptx::cuda_cu_device_get_attribute_func_t gpu::Ptx::_cuda_cu_device_get_attribute; | 45 gpu::Ptx::cuda_cu_device_get_attribute_func_t gpu::Ptx::_cuda_cu_device_get_attribute; |
44 gpu::Ptx::cuda_cu_launch_kernel_func_t gpu::Ptx::_cuda_cu_launch_kernel; | 46 gpu::Ptx::cuda_cu_launch_kernel_func_t gpu::Ptx::_cuda_cu_launch_kernel; |
45 gpu::Ptx::cuda_cu_module_get_function_func_t gpu::Ptx::_cuda_cu_module_get_function; | 47 gpu::Ptx::cuda_cu_module_get_function_func_t gpu::Ptx::_cuda_cu_module_get_function; |
46 gpu::Ptx::cuda_cu_module_load_data_ex_func_t gpu::Ptx::_cuda_cu_module_load_data_ex; | 48 gpu::Ptx::cuda_cu_module_load_data_ex_func_t gpu::Ptx::_cuda_cu_module_load_data_ex; |
49 gpu::Ptx::cuda_cu_memcpy_dtoh_func_t gpu::Ptx::_cuda_cu_memcpy_dtoh; | |
50 gpu::Ptx::cuda_cu_memfree_func_t gpu::Ptx::_cuda_cu_memfree; | |
47 | 51 |
48 void gpu::probe_linkage() { | 52 void gpu::probe_linkage() { |
49 #if defined(__APPLE__) || defined(LINUX) | 53 #if defined(__APPLE__) || defined(LINUX) |
50 set_gpu_linkage(gpu::Ptx::probe_linkage()); | 54 set_gpu_linkage(gpu::Ptx::probe_linkage()); |
51 #else | 55 #else |
65 } else { | 69 } else { |
66 return NULL; | 70 return NULL; |
67 } | 71 } |
68 } | 72 } |
69 | 73 |
70 bool gpu::execute_kernel(address kernel, JavaCallArguments * jca) { | 74 bool gpu::execute_kernel(address kernel, PTXKernelArguments & ptxka, JavaValue& ret) { |
71 if (gpu::has_gpu_linkage()) { | 75 if (gpu::has_gpu_linkage()) { |
72 return (gpu::Ptx::execute_kernel(kernel, jca)); | 76 return (gpu::Ptx::execute_kernel(kernel, ptxka, ret)); |
73 } else { | 77 } else { |
74 return false; | 78 return false; |
75 } | 79 } |
76 } | 80 } |
77 | 81 |
106 } | 110 } |
107 | 111 |
108 /* Get the handle to the first compute device */ | 112 /* Get the handle to the first compute device */ |
109 int device_id = 0; | 113 int device_id = 0; |
110 /* Compute-capable device handle */ | 114 /* Compute-capable device handle */ |
111 int cu_device = 0; | 115 status = _cuda_cu_device_get(&_cu_device, device_id); |
112 status = _cuda_cu_device_get(&cu_device, device_id); | |
113 | 116 |
114 if (status != GRAAL_CUDA_SUCCESS) { | 117 if (status != GRAAL_CUDA_SUCCESS) { |
115 tty->print_cr("[CUDA] Failed to get handle of first compute-capable device i.e., the one at ordinal: %d", device_id); | 118 tty->print_cr("[CUDA] Failed to get handle of first compute-capable device i.e., the one at ordinal: %d", device_id); |
116 return false; | 119 return false; |
117 } | 120 } |
120 tty->print_cr("[CUDA] Got the handle of first compute-device"); | 123 tty->print_cr("[CUDA] Got the handle of first compute-device"); |
121 } | 124 } |
122 | 125 |
123 /* Get device attributes */ | 126 /* Get device attributes */ |
124 int minor, major, unified_addressing; | 127 int minor, major, unified_addressing; |
125 status = _cuda_cu_device_get_attribute(&minor, GRAAL_CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR, cu_device); | 128 status = _cuda_cu_device_get_attribute(&minor, GRAAL_CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR, _cu_device); |
126 | 129 |
127 if (status != GRAAL_CUDA_SUCCESS) { | 130 if (status != GRAAL_CUDA_SUCCESS) { |
128 tty->print_cr("[CUDA] Failed to get minor attribute of device: %d", cu_device); | 131 tty->print_cr("[CUDA] Failed to get minor attribute of device: %d", _cu_device); |
129 return false; | 132 return false; |
130 } | 133 } |
131 | 134 |
132 status = _cuda_cu_device_get_attribute(&major, GRAAL_CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR, cu_device); | 135 status = _cuda_cu_device_get_attribute(&major, GRAAL_CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR, _cu_device); |
133 | 136 |
134 if (status != GRAAL_CUDA_SUCCESS) { | 137 if (status != GRAAL_CUDA_SUCCESS) { |
135 tty->print_cr("[CUDA] Failed to get major attribute of device: %d", cu_device); | 138 tty->print_cr("[CUDA] Failed to get major attribute of device: %d", _cu_device); |
136 return false; | 139 return false; |
137 } | 140 } |
138 | 141 |
139 if (TraceGPUInteraction) { | 142 if (TraceGPUInteraction) { |
140 tty->print_cr("[CUDA] Compatibility version of device %d: %d.%d", cu_device, major, minor); | 143 tty->print_cr("[CUDA] Compatibility version of device %d: %d.%d", _cu_device, major, minor); |
141 } | 144 } |
142 | 145 |
143 status = _cuda_cu_device_get_attribute(&unified_addressing, GRAAL_CU_DEVICE_ATTRIBUTE_UNIFIED_ADDRESSING, cu_device); | 146 status = _cuda_cu_device_get_attribute(&unified_addressing, GRAAL_CU_DEVICE_ATTRIBUTE_UNIFIED_ADDRESSING, _cu_device); |
144 | 147 |
145 if (status != GRAAL_CUDA_SUCCESS) { | 148 if (status != GRAAL_CUDA_SUCCESS) { |
146 tty->print_cr("[CUDA] Failed to query unified addressing mode of device: %d", cu_device); | 149 tty->print_cr("[CUDA] Failed to query unified addressing mode of device: %d", _cu_device); |
147 return false; | 150 return false; |
148 } | 151 } |
149 | 152 |
150 if (TraceGPUInteraction) { | 153 if (TraceGPUInteraction) { |
151 tty->print_cr("[CUDA] Unified addressing support on device %d: %d", cu_device, unified_addressing); | 154 tty->print_cr("[CUDA] Unified addressing support on device %d: %d", _cu_device, unified_addressing); |
152 } | 155 } |
153 | 156 |
154 | 157 |
155 /* Get device name */ | 158 /* Get device name */ |
156 char device_name[256]; | 159 char device_name[256]; |
157 status = _cuda_cu_device_get_name(device_name, 256, cu_device); | 160 status = _cuda_cu_device_get_name(device_name, 256, _cu_device); |
158 | 161 |
159 if (status != GRAAL_CUDA_SUCCESS) { | 162 if (status != GRAAL_CUDA_SUCCESS) { |
160 tty->print_cr("[CUDA] Failed to get name of device: %d", cu_device); | 163 tty->print_cr("[CUDA] Failed to get name of device: %d", _cu_device); |
161 return false; | 164 return false; |
162 } | 165 } |
163 | 166 |
164 if (TraceGPUInteraction) { | 167 if (TraceGPUInteraction) { |
165 tty->print_cr("[CUDA] Using %s", device_name); | 168 tty->print_cr("[CUDA] Using %s", device_name); |
166 } | |
167 | |
168 /* Create CUDA context */ | |
169 status = _cuda_cu_ctx_create(&_device_context, 0, cu_device); | |
170 | |
171 if (status != GRAAL_CUDA_SUCCESS) { | |
172 tty->print_cr("[CUDA] Failed to create CUDA context for device: %d", cu_device); | |
173 return false; | |
174 } | |
175 | |
176 if (TraceGPUInteraction) { | |
177 tty->print_cr("[CUDA] Success: Created context for device: %d", cu_device); | |
178 } | 169 } |
179 | 170 |
180 return true; | 171 return true; |
181 } | 172 } |
182 | 173 |
208 tty->print_cr("[CUDA] PTX Kernel\n%s", code); | 199 tty->print_cr("[CUDA] PTX Kernel\n%s", code); |
209 tty->print_cr("[CUDA] Function name : %s", name); | 200 tty->print_cr("[CUDA] Function name : %s", name); |
210 | 201 |
211 } | 202 } |
212 | 203 |
204 /* Create CUDA context to compile and execute the kernel */ | |
205 int status = _cuda_cu_ctx_create(&_device_context, 0, _cu_device); | |
206 | |
207 if (status != GRAAL_CUDA_SUCCESS) { | |
208 tty->print_cr("[CUDA] Failed to create CUDA context for device: %d", _cu_device); | |
209 return NULL; | |
210 } | |
211 | |
212 if (TraceGPUInteraction) { | |
213 tty->print_cr("[CUDA] Success: Created context for device: %d", _cu_device); | |
214 } | |
215 | |
213 /* Load module's data with compiler options */ | 216 /* Load module's data with compiler options */ |
214 int status = _cuda_cu_module_load_data_ex(&cu_module, code, jit_num_options, | 217 status = _cuda_cu_module_load_data_ex(&cu_module, (void*) code, jit_num_options, |
215 jit_options, (void **)jit_option_values); | 218 jit_options, (void **)jit_option_values); |
216 if (status != GRAAL_CUDA_SUCCESS) { | 219 if (status != GRAAL_CUDA_SUCCESS) { |
217 if (status == GRAAL_CUDA_ERROR_NO_BINARY_FOR_GPU) { | 220 if (status == GRAAL_CUDA_ERROR_NO_BINARY_FOR_GPU) { |
218 tty->print_cr("[CUDA] Check for malformed PTX kernel or incorrect PTX compilation options"); | 221 tty->print_cr("[CUDA] Check for malformed PTX kernel or incorrect PTX compilation options"); |
219 } | 222 } |
236 } | 239 } |
237 | 240 |
238 if (TraceGPUInteraction) { | 241 if (TraceGPUInteraction) { |
239 tty->print_cr("[CUDA] Got function handle for %s", name); | 242 tty->print_cr("[CUDA] Got function handle for %s", name); |
240 } | 243 } |
244 | |
241 return cu_function; | 245 return cu_function; |
242 } | 246 } |
243 | 247 |
244 bool gpu::Ptx::execute_kernel(address kernel, JavaCallArguments * jca) { | 248 bool gpu::Ptx::execute_kernel(address kernel, PTXKernelArguments &ptxka, JavaValue &ret) { |
245 // grid dimensionality | 249 // grid dimensionality |
246 unsigned int gridX = 1; | 250 unsigned int gridX = 1; |
247 unsigned int gridY = 1; | 251 unsigned int gridY = 1; |
248 unsigned int gridZ = 1; | 252 unsigned int gridZ = 1; |
249 | 253 |
250 // thread dimensionality | 254 // thread dimensionality |
251 unsigned int blockX = 1; | 255 unsigned int blockX = 1; |
252 unsigned int blockY = 1; | 256 unsigned int blockY = 1; |
253 unsigned int blockZ = 1; | 257 unsigned int blockZ = 1; |
254 | 258 |
255 int *cu_function = (int *)kernel; | 259 struct CUfunc_st* cu_function = (struct CUfunc_st*) kernel; |
256 | 260 |
257 char * paramBuffer = (char *) jca->parameters(); | 261 void * config[5] = { |
258 size_t paramBufferSz = (size_t) jca->size_of_parameters(); | 262 GRAAL_CU_LAUNCH_PARAM_BUFFER_POINTER, ptxka._kernelArgBuffer, |
259 | 263 GRAAL_CU_LAUNCH_PARAM_BUFFER_SIZE, &(ptxka._bufferOffset), |
260 void * config[] = { | |
261 GRAAL_CU_LAUNCH_PARAM_BUFFER_POINTER, paramBuffer, | |
262 GRAAL_CU_LAUNCH_PARAM_BUFFER_SIZE, ¶mBufferSz, | |
263 GRAAL_CU_LAUNCH_PARAM_END | 264 GRAAL_CU_LAUNCH_PARAM_END |
264 }; | 265 }; |
265 | 266 |
266 if (kernel == NULL) { | 267 if (kernel == NULL) { |
267 return false; | 268 return false; |
268 } | 269 } |
269 | 270 |
270 if (TraceGPUInteraction) { | 271 if (TraceGPUInteraction) { |
271 tty->print_cr("[CUDA] launching kernel"); | 272 tty->print_cr("[CUDA] launching kernel"); |
272 } | 273 } |
274 | |
273 int status = _cuda_cu_launch_kernel(cu_function, | 275 int status = _cuda_cu_launch_kernel(cu_function, |
274 gridX, gridY, gridZ, | 276 gridX, gridY, gridZ, |
275 blockX, blockY, blockZ, | 277 blockX, blockY, blockZ, |
276 0, NULL, NULL, config); | 278 0, NULL, NULL, (void **) &config); |
277 if (status != GRAAL_CUDA_SUCCESS) { | 279 if (status != GRAAL_CUDA_SUCCESS) { |
278 tty->print_cr("[CUDA] Failed to launch kernel"); | 280 tty->print_cr("[CUDA] Failed to launch kernel"); |
279 return false; | 281 return false; |
280 } | 282 } |
281 | 283 |
282 if (TraceGPUInteraction) { | 284 if (TraceGPUInteraction) { |
283 tty->print_cr("[CUDA] Success: Kernel Launch"); | 285 tty->print_cr("[CUDA] Success: Kernel Launch"); |
284 } | 286 } |
285 return status == 0; // GRAAL_CUDA_SUCCESS | 287 |
288 status = _cuda_cu_ctx_synchronize(); | |
289 | |
290 if (status != GRAAL_CUDA_SUCCESS) { | |
291 tty->print_cr("[CUDA] Failed to synchronize launched kernel (%d)", status); | |
292 return false; | |
293 } | |
294 | |
295 if (TraceGPUInteraction) { | |
296 tty->print_cr("[CUDA] Success: Synchronized launch kernel"); | |
297 } | |
298 | |
299 | |
300 // Get the result. TODO: Move this code to get_return_oop() | |
301 BasicType return_type = ptxka.get_ret_type(); | |
302 switch (return_type) { | |
303 case T_INT : | |
304 { | |
305 int return_val; | |
306 status = gpu::Ptx::_cuda_cu_memcpy_dtoh(&return_val, ptxka._return_value_ptr, T_INT_BYTE_SIZE); | |
307 if (status != GRAAL_CUDA_SUCCESS) { | |
308 tty->print_cr("[CUDA] *** Error (%d) Failed to copy value to device argument", status); | |
309 return false; | |
310 } | |
311 ret.set_jint(return_val); | |
312 } | |
313 break; | |
314 case T_LONG : | |
315 { | |
316 long return_val; | |
317 status = gpu::Ptx::_cuda_cu_memcpy_dtoh(&return_val, ptxka._return_value_ptr, T_LONG_BYTE_SIZE); | |
318 if (status != GRAAL_CUDA_SUCCESS) { | |
319 tty->print_cr("[CUDA] *** Error (%d) Failed to copy value to device argument", status); | |
320 return false; | |
321 } | |
322 ret.set_jlong(return_val); | |
323 } | |
324 break; | |
325 default: | |
326 tty->print_cr("[CUDA] TODO *** Unhandled return type"); | |
327 } | |
328 | |
329 | |
330 // Free device memory allocated for result | |
331 status = gpu::Ptx::_cuda_cu_memfree(ptxka._return_value_ptr); | |
332 if (status != GRAAL_CUDA_SUCCESS) { | |
333 tty->print_cr("[CUDA] *** Error (%d) Failed to free device memory of return value", status); | |
334 return false; | |
335 } | |
336 | |
337 if (TraceGPUInteraction) { | |
338 tty->print_cr("[CUDA] Success: Freed device memory of return value"); | |
339 } | |
340 | |
341 // Destroy context | |
342 status = gpu::Ptx::_cuda_cu_ctx_destroy(_device_context); | |
343 if (status != GRAAL_CUDA_SUCCESS) { | |
344 tty->print_cr("[CUDA] *** Error (%d) Failed to destroy context", status); | |
345 return false; | |
346 } | |
347 | |
348 if (TraceGPUInteraction) { | |
349 tty->print_cr("[CUDA] Success: Destroy context"); | |
350 } | |
351 | |
352 return (status == GRAAL_CUDA_SUCCESS); | |
286 } | 353 } |
287 | 354 |
288 #if defined(LINUX) | 355 #if defined(LINUX) |
289 static const char cuda_library_name[] = "libcuda.so"; | 356 static const char cuda_library_name[] = "libcuda.so"; |
290 #elif defined(__APPLE__) | 357 #elif defined(__APPLE__) |
303 if (handle != NULL) { | 370 if (handle != NULL) { |
304 _cuda_cu_init = | 371 _cuda_cu_init = |
305 CAST_TO_FN_PTR(cuda_cu_init_func_t, os::dll_lookup(handle, "cuInit")); | 372 CAST_TO_FN_PTR(cuda_cu_init_func_t, os::dll_lookup(handle, "cuInit")); |
306 _cuda_cu_ctx_create = | 373 _cuda_cu_ctx_create = |
307 CAST_TO_FN_PTR(cuda_cu_ctx_create_func_t, os::dll_lookup(handle, "cuCtxCreate")); | 374 CAST_TO_FN_PTR(cuda_cu_ctx_create_func_t, os::dll_lookup(handle, "cuCtxCreate")); |
308 _cuda_cu_ctx_detach = | 375 _cuda_cu_ctx_destroy = |
309 CAST_TO_FN_PTR(cuda_cu_ctx_detach_func_t, os::dll_lookup(handle, "cuCtxDetach")); | 376 CAST_TO_FN_PTR(cuda_cu_ctx_destroy_func_t, os::dll_lookup(handle, "cuCtxDestroy")); |
310 _cuda_cu_ctx_synchronize = | 377 _cuda_cu_ctx_synchronize = |
311 CAST_TO_FN_PTR(cuda_cu_ctx_synchronize_func_t, os::dll_lookup(handle, "cuCtxSynchronize")); | 378 CAST_TO_FN_PTR(cuda_cu_ctx_synchronize_func_t, os::dll_lookup(handle, "cuCtxSynchronize")); |
312 _cuda_cu_device_get_count = | 379 _cuda_cu_device_get_count = |
313 CAST_TO_FN_PTR(cuda_cu_device_get_count_func_t, os::dll_lookup(handle, "cuDeviceGetCount")); | 380 CAST_TO_FN_PTR(cuda_cu_device_get_count_func_t, os::dll_lookup(handle, "cuDeviceGetCount")); |
314 _cuda_cu_device_get_name = | 381 _cuda_cu_device_get_name = |
323 CAST_TO_FN_PTR(cuda_cu_module_get_function_func_t, os::dll_lookup(handle, "cuModuleGetFunction")); | 390 CAST_TO_FN_PTR(cuda_cu_module_get_function_func_t, os::dll_lookup(handle, "cuModuleGetFunction")); |
324 _cuda_cu_module_load_data_ex = | 391 _cuda_cu_module_load_data_ex = |
325 CAST_TO_FN_PTR(cuda_cu_module_load_data_ex_func_t, os::dll_lookup(handle, "cuModuleLoadDataEx")); | 392 CAST_TO_FN_PTR(cuda_cu_module_load_data_ex_func_t, os::dll_lookup(handle, "cuModuleLoadDataEx")); |
326 _cuda_cu_launch_kernel = | 393 _cuda_cu_launch_kernel = |
327 CAST_TO_FN_PTR(cuda_cu_launch_kernel_func_t, os::dll_lookup(handle, "cuLaunchKernel")); | 394 CAST_TO_FN_PTR(cuda_cu_launch_kernel_func_t, os::dll_lookup(handle, "cuLaunchKernel")); |
395 _cuda_cu_memalloc = | |
396 CAST_TO_FN_PTR(cuda_cu_memalloc_func_t, os::dll_lookup(handle, "cuMemAlloc")); | |
397 _cuda_cu_memfree = | |
398 CAST_TO_FN_PTR(cuda_cu_memfree_func_t, os::dll_lookup(handle, "cuMemFree")); | |
399 _cuda_cu_memcpy_htod = | |
400 CAST_TO_FN_PTR(cuda_cu_memcpy_htod_func_t, os::dll_lookup(handle, "cuMemcpyHtoD")); | |
401 _cuda_cu_memcpy_dtoh = | |
402 CAST_TO_FN_PTR(cuda_cu_memcpy_dtoh_func_t, os::dll_lookup(handle, "cuMemcpyDtoH")); | |
403 | |
328 if (TraceGPUInteraction) { | 404 if (TraceGPUInteraction) { |
329 tty->print_cr("[CUDA] Success: library linkage"); | 405 tty->print_cr("[CUDA] Success: library linkage"); |
330 } | 406 } |
331 return true; | 407 return true; |
332 } else { | 408 } else { |