Mercurial > hg > truffle
comparison src/gpu/ptx/vm/gpu_ptx.cpp @ 10879:d55f24eac4b1
PTX support for Linux
author | Morris Meyer <morris.meyer@oracle.com> |
---|---|
date | Thu, 25 Jul 2013 22:15:30 -0400 |
parents | |
children | 6a2d65cb5d7d |
comparison
equal
deleted
inserted
replaced
10878:d9fcc82766da | 10879:d55f24eac4b1 |
---|---|
1 /* | |
2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. | |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
20 * or visit www.oracle.com if you need additional information or have any | |
21 * questions. | |
22 * | |
23 */ | |
24 | |
25 #include "precompiled.hpp" | |
26 #include "runtime/gpu.hpp" | |
27 #include "utilities/globalDefinitions.hpp" | |
28 #include "utilities/ostream.hpp" | |
29 | |
30 void * gpu::Ptx::_device_context; | |
31 | |
32 gpu::Ptx::cuda_cu_init_func_t gpu::Ptx::_cuda_cu_init; | |
33 gpu::Ptx::cuda_cu_ctx_create_func_t gpu::Ptx::_cuda_cu_ctx_create; | |
34 gpu::Ptx::cuda_cu_ctx_detach_func_t gpu::Ptx::_cuda_cu_ctx_detach; | |
35 gpu::Ptx::cuda_cu_ctx_synchronize_func_t gpu::Ptx::_cuda_cu_ctx_synchronize; | |
36 gpu::Ptx::cuda_cu_device_get_count_func_t gpu::Ptx::_cuda_cu_device_get_count; | |
37 gpu::Ptx::cuda_cu_device_get_name_func_t gpu::Ptx::_cuda_cu_device_get_name; | |
38 gpu::Ptx::cuda_cu_device_get_func_t gpu::Ptx::_cuda_cu_device_get; | |
39 gpu::Ptx::cuda_cu_device_compute_capability_func_t gpu::Ptx::_cuda_cu_device_compute_capability; | |
40 gpu::Ptx::cuda_cu_device_get_attribute_func_t gpu::Ptx::_cuda_cu_device_get_attribute; | |
41 gpu::Ptx::cuda_cu_launch_kernel_func_t gpu::Ptx::_cuda_cu_launch_kernel; | |
42 gpu::Ptx::cuda_cu_module_get_function_func_t gpu::Ptx::_cuda_cu_module_get_function; | |
43 gpu::Ptx::cuda_cu_module_load_data_ex_func_t gpu::Ptx::_cuda_cu_module_load_data_ex; | |
44 | |
45 void gpu::probe_linkage() { | |
46 #if defined(__APPLE__) || defined(LINUX) | |
47 set_gpu_linkage(gpu::Ptx::probe_linkage()); | |
48 #else | |
49 set_gpu_linkage(false); | |
50 #endif | |
51 } | |
52 | |
53 void gpu::initialize_gpu() { | |
54 if (gpu::has_gpu_linkage()) { | |
55 set_initialized(gpu::Ptx::initialize_gpu()); | |
56 } | |
57 } | |
58 | |
59 void * gpu::generate_kernel(unsigned char *code, int code_len, const char *name) { | |
60 if (gpu::has_gpu_linkage()) { | |
61 return (gpu::Ptx::generate_kernel(code, code_len, name)); | |
62 } else { | |
63 return NULL; | |
64 } | |
65 } | |
66 | |
67 bool gpu::execute_kernel(address kernel) { | |
68 if (gpu::has_gpu_linkage()) { | |
69 return (gpu::Ptx::execute_kernel(kernel)); | |
70 } else { | |
71 return false; | |
72 } | |
73 } | |
74 | |
75 bool gpu::Ptx::initialize_gpu() { | |
76 | |
77 /* Initialize CUDA driver API */ | |
78 int status = _cuda_cu_init(0); | |
79 if (status != GRAAL_CUDA_SUCCESS) { | |
80 tty->print_cr("Failed to initialize CUDA device"); | |
81 return false; | |
82 } | |
83 | |
84 if (TraceGPUInteraction) { | |
85 tty->print_cr("CUDA driver initialization: Success"); | |
86 } | |
87 | |
88 /* Get the number of compute-capable device count */ | |
89 int device_count = 0; | |
90 status = _cuda_cu_device_get_count(&device_count); | |
91 if (status != GRAAL_CUDA_SUCCESS) { | |
92 tty->print_cr("[CUDA] Failed to get compute-capable device count"); | |
93 return false; | |
94 } | |
95 | |
96 if (device_count == 0) { | |
97 tty->print_cr("[CUDA] Found no device supporting CUDA"); | |
98 return false; | |
99 } | |
100 | |
101 if (TraceGPUInteraction) { | |
102 tty->print_cr("[CUDA] Number of compute-capable devices found: %d", device_count); | |
103 } | |
104 | |
105 /* Get the handle to the first compute device */ | |
106 int device_id = 0; | |
107 /* Compute-capable device handle */ | |
108 int cu_device = 0; | |
109 status = _cuda_cu_device_get(&cu_device, device_id); | |
110 | |
111 if (status != GRAAL_CUDA_SUCCESS) { | |
112 tty->print_cr("[CUDA] Failed to get handle of first compute-capable device i.e., the one at ordinal: %d", device_id); | |
113 return false; | |
114 } | |
115 | |
116 if (TraceGPUInteraction) { | |
117 tty->print_cr("[CUDA] Got the handle of first compute-device"); | |
118 } | |
119 | |
120 /* Get device attributes */ | |
121 int minor, major; | |
122 status = _cuda_cu_device_get_attribute(&minor, GRAAL_CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR, cu_device); | |
123 | |
124 if (status != GRAAL_CUDA_SUCCESS) { | |
125 tty->print_cr("[CUDA] Failed to get minor attribute of device: %d", cu_device); | |
126 return false; | |
127 } | |
128 | |
129 status = _cuda_cu_device_get_attribute(&major, GRAAL_CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR, cu_device); | |
130 | |
131 if (status != GRAAL_CUDA_SUCCESS) { | |
132 tty->print_cr("[CUDA] Failed to get major attribute of device: %d", cu_device); | |
133 return false; | |
134 } | |
135 | |
136 if (TraceGPUInteraction) { | |
137 tty->print_cr("[CUDA] Compatibility version of device %d: %d.%d", cu_device, major, minor); | |
138 } | |
139 | |
140 /* Get device name */ | |
141 char device_name[256]; | |
142 status = _cuda_cu_device_get_name(device_name, 256, cu_device); | |
143 | |
144 if (status != GRAAL_CUDA_SUCCESS) { | |
145 tty->print_cr("[CUDA] Failed to get name of device: %d", cu_device); | |
146 return false; | |
147 } | |
148 | |
149 if (TraceGPUInteraction) { | |
150 tty->print_cr("[CUDA] Using %s", device_name); | |
151 } | |
152 | |
153 /* Create CUDA context */ | |
154 status = _cuda_cu_ctx_create(&_device_context, 0, cu_device); | |
155 | |
156 if (status != GRAAL_CUDA_SUCCESS) { | |
157 tty->print_cr("[CUDA] Failed to create CUDA context for device: %d", cu_device); | |
158 return false; | |
159 } | |
160 | |
161 if (TraceGPUInteraction) { | |
162 tty->print_cr("[CUDA] Success: Created context for device: %d", cu_device); | |
163 } | |
164 | |
165 return true; | |
166 } | |
167 | |
168 void *gpu::Ptx::generate_kernel(unsigned char *code, int code_len, const char *name) { | |
169 | |
170 struct CUmod_st * cu_module; | |
171 // Use three JIT compiler options | |
172 const unsigned int jit_num_options = 3; | |
173 int *jit_options = NEW_C_HEAP_ARRAY(int, jit_num_options, mtCompiler); | |
174 void **jit_option_values = NEW_C_HEAP_ARRAY(void *, jit_num_options, mtCompiler); | |
175 | |
176 // Set up PTX JIT compiler options | |
177 // 1. set size of compilation log buffer | |
178 int jit_log_buffer_size = 1024; | |
179 jit_options[0] = GRAAL_CU_JIT_INFO_LOG_BUFFER_SIZE_BYTES; | |
180 jit_option_values[0] = (void *)(size_t)jit_log_buffer_size; | |
181 | |
182 // 2. set pointer to compilation log buffer | |
183 char *jit_log_buffer = NEW_C_HEAP_ARRAY(char, jit_log_buffer_size, mtCompiler); | |
184 jit_options[1] = GRAAL_CU_JIT_INFO_LOG_BUFFER; | |
185 jit_option_values[1] = jit_log_buffer; | |
186 | |
187 // 3. set pointer to set the Maximum # of registers (32) for the kernel | |
188 int jit_register_count = 32; | |
189 jit_options[2] = GRAAL_CU_JIT_MAX_REGISTERS; | |
190 jit_option_values[2] = (void *)(size_t)jit_register_count; | |
191 | |
192 if (TraceGPUInteraction) { | |
193 tty->print_cr("[CUDA] PTX Kernel\n%s", code); | |
194 tty->print_cr("[CUDA] Function name : %s", name); | |
195 | |
196 } | |
197 | |
198 /* Load module's data with compiler options */ | |
199 int status = _cuda_cu_module_load_data_ex(&cu_module, code, jit_num_options, | |
200 jit_options, (void **)jit_option_values); | |
201 if (status != GRAAL_CUDA_SUCCESS) { | |
202 if (status == GRAAL_CUDA_ERROR_NO_BINARY_FOR_GPU) { | |
203 tty->print_cr("[CUDA] Check for malformed PTX kernel or incorrect PTX compilation options"); | |
204 } | |
205 tty->print_cr("[CUDA] *** Error (%d) Failed to load module data with online compiler options for method %s", | |
206 status, name); | |
207 return NULL; | |
208 } | |
209 | |
210 if (TraceGPUInteraction) { | |
211 tty->print_cr("[CUDA] Loaded data for PTX Kernel"); | |
212 } | |
213 | |
214 struct CUfunc_st * cu_function; | |
215 | |
216 status = _cuda_cu_module_get_function(&cu_function, cu_module, name); | |
217 | |
218 if (status != GRAAL_CUDA_SUCCESS) { | |
219 tty->print_cr("[CUDA] Failed to get function %s", name); | |
220 return NULL; | |
221 } | |
222 | |
223 if (TraceGPUInteraction) { | |
224 tty->print_cr("[CUDA] Got function handle for %s", name); | |
225 } | |
226 return cu_function; | |
227 } | |
228 | |
229 bool gpu::Ptx::execute_kernel(address kernel) { | |
230 // grid dimensionality | |
231 unsigned int gridX = 1; | |
232 unsigned int gridY = 1; | |
233 unsigned int gridZ = 1; | |
234 | |
235 // thread dimensionality | |
236 unsigned int blockX = 1; | |
237 unsigned int blockY = 1; | |
238 unsigned int blockZ = 1; | |
239 | |
240 int *cu_function = (int *)kernel; | |
241 | |
242 if (kernel == NULL) { | |
243 return false; | |
244 } | |
245 | |
246 if (TraceGPUInteraction) { | |
247 tty->print_cr("[CUDA] launching kernel"); | |
248 } | |
249 int status = _cuda_cu_launch_kernel(cu_function, | |
250 gridX, gridY, gridZ, | |
251 blockX, blockY, blockZ, | |
252 0, NULL, NULL, NULL); | |
253 if (status != GRAAL_CUDA_SUCCESS) { | |
254 tty->print_cr("[CUDA] Failed to launch kernel"); | |
255 return false; | |
256 } | |
257 | |
258 if (TraceGPUInteraction) { | |
259 tty->print_cr("[CUDA] Success: Kernel Launch"); | |
260 } | |
261 return status == 0; // GRAAL_CUDA_SUCCESS | |
262 } | |
263 | |
264 #if defined(LINUX) | |
265 static const char cuda_library_name[] = "libcuda.so"; | |
266 #elif defined(__APPLE__) | |
267 static char const cuda_library_name[] = "/usr/local/cuda/lib/libcuda.dylib"; | |
268 #else | |
269 static char const cuda_library_name[] = ""; | |
270 #endif | |
271 | |
272 bool gpu::Ptx::probe_linkage() { | |
273 if (cuda_library_name != NULL) { | |
274 void *handle = dlopen(cuda_library_name, RTLD_LAZY); | |
275 if (handle != NULL) { | |
276 _cuda_cu_init = | |
277 CAST_TO_FN_PTR(cuda_cu_init_func_t, dlsym(handle, "cuInit")); | |
278 _cuda_cu_ctx_create = | |
279 CAST_TO_FN_PTR(cuda_cu_ctx_create_func_t, dlsym(handle, "cuCtxCreate")); | |
280 _cuda_cu_ctx_detach = | |
281 CAST_TO_FN_PTR(cuda_cu_ctx_detach_func_t, dlsym(handle, "cuCtxDetach")); | |
282 _cuda_cu_ctx_synchronize = | |
283 CAST_TO_FN_PTR(cuda_cu_ctx_synchronize_func_t, dlsym(handle, "cuCtxSynchronize")); | |
284 _cuda_cu_device_get_count = | |
285 CAST_TO_FN_PTR(cuda_cu_device_get_count_func_t, dlsym(handle, "cuDeviceGetCount")); | |
286 _cuda_cu_device_get_name = | |
287 CAST_TO_FN_PTR(cuda_cu_device_get_name_func_t, dlsym(handle, "cuDeviceGetName")); | |
288 _cuda_cu_device_get = | |
289 CAST_TO_FN_PTR(cuda_cu_device_get_func_t, dlsym(handle, "cuDeviceGet")); | |
290 _cuda_cu_device_compute_capability = | |
291 CAST_TO_FN_PTR(cuda_cu_device_compute_capability_func_t, dlsym(handle, "cuDeviceComputeCapability")); | |
292 _cuda_cu_device_get_attribute = | |
293 CAST_TO_FN_PTR(cuda_cu_device_get_attribute_func_t, dlsym(handle, "cuDeviceGetAttribute")); | |
294 _cuda_cu_module_get_function = | |
295 CAST_TO_FN_PTR(cuda_cu_module_get_function_func_t, dlsym(handle, "cuModuleGetFunction")); | |
296 _cuda_cu_module_load_data_ex = | |
297 CAST_TO_FN_PTR(cuda_cu_module_load_data_ex_func_t, dlsym(handle, "cuModuleLoadDataEx")); | |
298 _cuda_cu_launch_kernel = | |
299 CAST_TO_FN_PTR(cuda_cu_launch_kernel_func_t, dlsym(handle, "cuLaunchKernel")); | |
300 if (TraceGPUInteraction) { | |
301 tty->print_cr("[CUDA] Success: library linkage"); | |
302 } | |
303 return true; | |
304 } else { | |
305 // Unable to dlopen libcuda | |
306 tty->print_cr("Use LD_LIBRARY_PATH (or other means) to specify installed location of CUDA library"); | |
307 return false; | |
308 } | |
309 } else { | |
310 tty->print_cr("Unsupported CUDA platform"); | |
311 return false; | |
312 } | |
313 tty->print_cr("Failed to find CUDA linkage"); | |
314 return false; | |
315 } | |
316 |