Mercurial > hg > graal-compiler
annotate src/gpu/ptx/vm/gpu_ptx.cpp @ 10953:97e282186b5b
Add heap sanity checker with premature hard crash for debugging write barriers
author | Christos Kotselidis <christos.kotselidis@oracle.com> |
---|---|
date | Fri, 02 Aug 2013 19:03:05 +0200 |
parents | fe49e5121768 |
children | 1cd1f8ff70a1 |
rev | line source |
---|---|
10879 | 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" | |
10883
5fcb30bcb90a
Fix compilation without precompiled headers.
Roland Schatz <roland.schatz@oracle.com>
parents:
10882
diff
changeset
|
29 #include "memory/allocation.hpp" |
5fcb30bcb90a
Fix compilation without precompiled headers.
Roland Schatz <roland.schatz@oracle.com>
parents:
10882
diff
changeset
|
30 #include "memory/allocation.inline.hpp" |
10879 | 31 |
32 void * gpu::Ptx::_device_context; | |
33 | |
34 gpu::Ptx::cuda_cu_init_func_t gpu::Ptx::_cuda_cu_init; | |
35 gpu::Ptx::cuda_cu_ctx_create_func_t gpu::Ptx::_cuda_cu_ctx_create; | |
36 gpu::Ptx::cuda_cu_ctx_detach_func_t gpu::Ptx::_cuda_cu_ctx_detach; | |
37 gpu::Ptx::cuda_cu_ctx_synchronize_func_t gpu::Ptx::_cuda_cu_ctx_synchronize; | |
38 gpu::Ptx::cuda_cu_device_get_count_func_t gpu::Ptx::_cuda_cu_device_get_count; | |
39 gpu::Ptx::cuda_cu_device_get_name_func_t gpu::Ptx::_cuda_cu_device_get_name; | |
40 gpu::Ptx::cuda_cu_device_get_func_t gpu::Ptx::_cuda_cu_device_get; | |
41 gpu::Ptx::cuda_cu_device_compute_capability_func_t gpu::Ptx::_cuda_cu_device_compute_capability; | |
42 gpu::Ptx::cuda_cu_device_get_attribute_func_t gpu::Ptx::_cuda_cu_device_get_attribute; | |
43 gpu::Ptx::cuda_cu_launch_kernel_func_t gpu::Ptx::_cuda_cu_launch_kernel; | |
44 gpu::Ptx::cuda_cu_module_get_function_func_t gpu::Ptx::_cuda_cu_module_get_function; | |
45 gpu::Ptx::cuda_cu_module_load_data_ex_func_t gpu::Ptx::_cuda_cu_module_load_data_ex; | |
46 | |
47 void gpu::probe_linkage() { | |
48 #if defined(__APPLE__) || defined(LINUX) | |
49 set_gpu_linkage(gpu::Ptx::probe_linkage()); | |
50 #else | |
51 set_gpu_linkage(false); | |
52 #endif | |
53 } | |
54 | |
55 void gpu::initialize_gpu() { | |
56 if (gpu::has_gpu_linkage()) { | |
57 set_initialized(gpu::Ptx::initialize_gpu()); | |
58 } | |
59 } | |
60 | |
61 void * gpu::generate_kernel(unsigned char *code, int code_len, const char *name) { | |
62 if (gpu::has_gpu_linkage()) { | |
63 return (gpu::Ptx::generate_kernel(code, code_len, name)); | |
64 } else { | |
65 return NULL; | |
66 } | |
67 } | |
68 | |
69 bool gpu::execute_kernel(address kernel) { | |
70 if (gpu::has_gpu_linkage()) { | |
71 return (gpu::Ptx::execute_kernel(kernel)); | |
72 } else { | |
73 return false; | |
74 } | |
75 } | |
76 | |
77 bool gpu::Ptx::initialize_gpu() { | |
78 | |
79 /* Initialize CUDA driver API */ | |
80 int status = _cuda_cu_init(0); | |
81 if (status != GRAAL_CUDA_SUCCESS) { | |
82 tty->print_cr("Failed to initialize CUDA device"); | |
83 return false; | |
84 } | |
85 | |
86 if (TraceGPUInteraction) { | |
87 tty->print_cr("CUDA driver initialization: Success"); | |
88 } | |
89 | |
90 /* Get the number of compute-capable device count */ | |
91 int device_count = 0; | |
92 status = _cuda_cu_device_get_count(&device_count); | |
93 if (status != GRAAL_CUDA_SUCCESS) { | |
94 tty->print_cr("[CUDA] Failed to get compute-capable device count"); | |
95 return false; | |
96 } | |
97 | |
98 if (device_count == 0) { | |
99 tty->print_cr("[CUDA] Found no device supporting CUDA"); | |
100 return false; | |
101 } | |
102 | |
103 if (TraceGPUInteraction) { | |
104 tty->print_cr("[CUDA] Number of compute-capable devices found: %d", device_count); | |
105 } | |
106 | |
107 /* Get the handle to the first compute device */ | |
108 int device_id = 0; | |
109 /* Compute-capable device handle */ | |
110 int cu_device = 0; | |
111 status = _cuda_cu_device_get(&cu_device, device_id); | |
112 | |
113 if (status != GRAAL_CUDA_SUCCESS) { | |
114 tty->print_cr("[CUDA] Failed to get handle of first compute-capable device i.e., the one at ordinal: %d", device_id); | |
115 return false; | |
116 } | |
117 | |
118 if (TraceGPUInteraction) { | |
119 tty->print_cr("[CUDA] Got the handle of first compute-device"); | |
120 } | |
121 | |
122 /* Get device attributes */ | |
123 int minor, major; | |
124 status = _cuda_cu_device_get_attribute(&minor, GRAAL_CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR, cu_device); | |
125 | |
126 if (status != GRAAL_CUDA_SUCCESS) { | |
127 tty->print_cr("[CUDA] Failed to get minor attribute of device: %d", cu_device); | |
128 return false; | |
129 } | |
130 | |
131 status = _cuda_cu_device_get_attribute(&major, GRAAL_CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR, cu_device); | |
132 | |
133 if (status != GRAAL_CUDA_SUCCESS) { | |
134 tty->print_cr("[CUDA] Failed to get major attribute of device: %d", cu_device); | |
135 return false; | |
136 } | |
137 | |
138 if (TraceGPUInteraction) { | |
139 tty->print_cr("[CUDA] Compatibility version of device %d: %d.%d", cu_device, major, minor); | |
140 } | |
141 | |
142 /* Get device name */ | |
143 char device_name[256]; | |
144 status = _cuda_cu_device_get_name(device_name, 256, cu_device); | |
145 | |
146 if (status != GRAAL_CUDA_SUCCESS) { | |
147 tty->print_cr("[CUDA] Failed to get name of device: %d", cu_device); | |
148 return false; | |
149 } | |
150 | |
151 if (TraceGPUInteraction) { | |
152 tty->print_cr("[CUDA] Using %s", device_name); | |
153 } | |
154 | |
155 /* Create CUDA context */ | |
156 status = _cuda_cu_ctx_create(&_device_context, 0, cu_device); | |
157 | |
158 if (status != GRAAL_CUDA_SUCCESS) { | |
159 tty->print_cr("[CUDA] Failed to create CUDA context for device: %d", cu_device); | |
160 return false; | |
161 } | |
162 | |
163 if (TraceGPUInteraction) { | |
164 tty->print_cr("[CUDA] Success: Created context for device: %d", cu_device); | |
165 } | |
166 | |
167 return true; | |
168 } | |
169 | |
170 void *gpu::Ptx::generate_kernel(unsigned char *code, int code_len, const char *name) { | |
171 | |
172 struct CUmod_st * cu_module; | |
173 // Use three JIT compiler options | |
174 const unsigned int jit_num_options = 3; | |
175 int *jit_options = NEW_C_HEAP_ARRAY(int, jit_num_options, mtCompiler); | |
176 void **jit_option_values = NEW_C_HEAP_ARRAY(void *, jit_num_options, mtCompiler); | |
177 | |
178 // Set up PTX JIT compiler options | |
179 // 1. set size of compilation log buffer | |
180 int jit_log_buffer_size = 1024; | |
181 jit_options[0] = GRAAL_CU_JIT_INFO_LOG_BUFFER_SIZE_BYTES; | |
182 jit_option_values[0] = (void *)(size_t)jit_log_buffer_size; | |
183 | |
184 // 2. set pointer to compilation log buffer | |
185 char *jit_log_buffer = NEW_C_HEAP_ARRAY(char, jit_log_buffer_size, mtCompiler); | |
186 jit_options[1] = GRAAL_CU_JIT_INFO_LOG_BUFFER; | |
187 jit_option_values[1] = jit_log_buffer; | |
188 | |
189 // 3. set pointer to set the Maximum # of registers (32) for the kernel | |
190 int jit_register_count = 32; | |
191 jit_options[2] = GRAAL_CU_JIT_MAX_REGISTERS; | |
192 jit_option_values[2] = (void *)(size_t)jit_register_count; | |
193 | |
194 if (TraceGPUInteraction) { | |
195 tty->print_cr("[CUDA] PTX Kernel\n%s", code); | |
196 tty->print_cr("[CUDA] Function name : %s", name); | |
197 | |
198 } | |
199 | |
200 /* Load module's data with compiler options */ | |
201 int status = _cuda_cu_module_load_data_ex(&cu_module, code, jit_num_options, | |
202 jit_options, (void **)jit_option_values); | |
203 if (status != GRAAL_CUDA_SUCCESS) { | |
204 if (status == GRAAL_CUDA_ERROR_NO_BINARY_FOR_GPU) { | |
205 tty->print_cr("[CUDA] Check for malformed PTX kernel or incorrect PTX compilation options"); | |
206 } | |
207 tty->print_cr("[CUDA] *** Error (%d) Failed to load module data with online compiler options for method %s", | |
208 status, name); | |
209 return NULL; | |
210 } | |
211 | |
212 if (TraceGPUInteraction) { | |
213 tty->print_cr("[CUDA] Loaded data for PTX Kernel"); | |
214 } | |
215 | |
216 struct CUfunc_st * cu_function; | |
217 | |
218 status = _cuda_cu_module_get_function(&cu_function, cu_module, name); | |
219 | |
220 if (status != GRAAL_CUDA_SUCCESS) { | |
221 tty->print_cr("[CUDA] Failed to get function %s", name); | |
222 return NULL; | |
223 } | |
224 | |
225 if (TraceGPUInteraction) { | |
226 tty->print_cr("[CUDA] Got function handle for %s", name); | |
227 } | |
228 return cu_function; | |
229 } | |
230 | |
231 bool gpu::Ptx::execute_kernel(address kernel) { | |
232 // grid dimensionality | |
233 unsigned int gridX = 1; | |
234 unsigned int gridY = 1; | |
235 unsigned int gridZ = 1; | |
236 | |
237 // thread dimensionality | |
238 unsigned int blockX = 1; | |
239 unsigned int blockY = 1; | |
240 unsigned int blockZ = 1; | |
241 | |
242 int *cu_function = (int *)kernel; | |
243 | |
244 if (kernel == NULL) { | |
245 return false; | |
246 } | |
247 | |
248 if (TraceGPUInteraction) { | |
249 tty->print_cr("[CUDA] launching kernel"); | |
250 } | |
251 int status = _cuda_cu_launch_kernel(cu_function, | |
252 gridX, gridY, gridZ, | |
253 blockX, blockY, blockZ, | |
254 0, NULL, NULL, NULL); | |
255 if (status != GRAAL_CUDA_SUCCESS) { | |
256 tty->print_cr("[CUDA] Failed to launch kernel"); | |
257 return false; | |
258 } | |
259 | |
260 if (TraceGPUInteraction) { | |
261 tty->print_cr("[CUDA] Success: Kernel Launch"); | |
262 } | |
263 return status == 0; // GRAAL_CUDA_SUCCESS | |
264 } | |
265 | |
266 #if defined(LINUX) | |
267 static const char cuda_library_name[] = "libcuda.so"; | |
268 #elif defined(__APPLE__) | |
269 static char const cuda_library_name[] = "/usr/local/cuda/lib/libcuda.dylib"; | |
270 #else | |
271 static char const cuda_library_name[] = ""; | |
272 #endif | |
273 | |
10882
6a2d65cb5d7d
fix: gpu_ptx.cpp did not compile on windows
Christian Wirth <christian.wirth@oracle.com>
parents:
10879
diff
changeset
|
274 #define STD_BUFFER_SIZE 1024 |
6a2d65cb5d7d
fix: gpu_ptx.cpp did not compile on windows
Christian Wirth <christian.wirth@oracle.com>
parents:
10879
diff
changeset
|
275 |
10879 | 276 bool gpu::Ptx::probe_linkage() { |
277 if (cuda_library_name != NULL) { | |
10882
6a2d65cb5d7d
fix: gpu_ptx.cpp did not compile on windows
Christian Wirth <christian.wirth@oracle.com>
parents:
10879
diff
changeset
|
278 char *buffer = (char*)malloc(STD_BUFFER_SIZE); |
6a2d65cb5d7d
fix: gpu_ptx.cpp did not compile on windows
Christian Wirth <christian.wirth@oracle.com>
parents:
10879
diff
changeset
|
279 void *handle = os::dll_load(cuda_library_name, buffer, STD_BUFFER_SIZE); |
6a2d65cb5d7d
fix: gpu_ptx.cpp did not compile on windows
Christian Wirth <christian.wirth@oracle.com>
parents:
10879
diff
changeset
|
280 free(buffer); |
10879 | 281 if (handle != NULL) { |
282 _cuda_cu_init = | |
10882
6a2d65cb5d7d
fix: gpu_ptx.cpp did not compile on windows
Christian Wirth <christian.wirth@oracle.com>
parents:
10879
diff
changeset
|
283 CAST_TO_FN_PTR(cuda_cu_init_func_t, os::dll_lookup(handle, "cuInit")); |
10879 | 284 _cuda_cu_ctx_create = |
10882
6a2d65cb5d7d
fix: gpu_ptx.cpp did not compile on windows
Christian Wirth <christian.wirth@oracle.com>
parents:
10879
diff
changeset
|
285 CAST_TO_FN_PTR(cuda_cu_ctx_create_func_t, os::dll_lookup(handle, "cuCtxCreate")); |
10879 | 286 _cuda_cu_ctx_detach = |
10882
6a2d65cb5d7d
fix: gpu_ptx.cpp did not compile on windows
Christian Wirth <christian.wirth@oracle.com>
parents:
10879
diff
changeset
|
287 CAST_TO_FN_PTR(cuda_cu_ctx_detach_func_t, os::dll_lookup(handle, "cuCtxDetach")); |
10879 | 288 _cuda_cu_ctx_synchronize = |
10882
6a2d65cb5d7d
fix: gpu_ptx.cpp did not compile on windows
Christian Wirth <christian.wirth@oracle.com>
parents:
10879
diff
changeset
|
289 CAST_TO_FN_PTR(cuda_cu_ctx_synchronize_func_t, os::dll_lookup(handle, "cuCtxSynchronize")); |
10879 | 290 _cuda_cu_device_get_count = |
10882
6a2d65cb5d7d
fix: gpu_ptx.cpp did not compile on windows
Christian Wirth <christian.wirth@oracle.com>
parents:
10879
diff
changeset
|
291 CAST_TO_FN_PTR(cuda_cu_device_get_count_func_t, os::dll_lookup(handle, "cuDeviceGetCount")); |
10879 | 292 _cuda_cu_device_get_name = |
10882
6a2d65cb5d7d
fix: gpu_ptx.cpp did not compile on windows
Christian Wirth <christian.wirth@oracle.com>
parents:
10879
diff
changeset
|
293 CAST_TO_FN_PTR(cuda_cu_device_get_name_func_t, os::dll_lookup(handle, "cuDeviceGetName")); |
10879 | 294 _cuda_cu_device_get = |
10882
6a2d65cb5d7d
fix: gpu_ptx.cpp did not compile on windows
Christian Wirth <christian.wirth@oracle.com>
parents:
10879
diff
changeset
|
295 CAST_TO_FN_PTR(cuda_cu_device_get_func_t, os::dll_lookup(handle, "cuDeviceGet")); |
10879 | 296 _cuda_cu_device_compute_capability = |
10882
6a2d65cb5d7d
fix: gpu_ptx.cpp did not compile on windows
Christian Wirth <christian.wirth@oracle.com>
parents:
10879
diff
changeset
|
297 CAST_TO_FN_PTR(cuda_cu_device_compute_capability_func_t, os::dll_lookup(handle, "cuDeviceComputeCapability")); |
10879 | 298 _cuda_cu_device_get_attribute = |
10882
6a2d65cb5d7d
fix: gpu_ptx.cpp did not compile on windows
Christian Wirth <christian.wirth@oracle.com>
parents:
10879
diff
changeset
|
299 CAST_TO_FN_PTR(cuda_cu_device_get_attribute_func_t, os::dll_lookup(handle, "cuDeviceGetAttribute")); |
10879 | 300 _cuda_cu_module_get_function = |
10882
6a2d65cb5d7d
fix: gpu_ptx.cpp did not compile on windows
Christian Wirth <christian.wirth@oracle.com>
parents:
10879
diff
changeset
|
301 CAST_TO_FN_PTR(cuda_cu_module_get_function_func_t, os::dll_lookup(handle, "cuModuleGetFunction")); |
10879 | 302 _cuda_cu_module_load_data_ex = |
10882
6a2d65cb5d7d
fix: gpu_ptx.cpp did not compile on windows
Christian Wirth <christian.wirth@oracle.com>
parents:
10879
diff
changeset
|
303 CAST_TO_FN_PTR(cuda_cu_module_load_data_ex_func_t, os::dll_lookup(handle, "cuModuleLoadDataEx")); |
10879 | 304 _cuda_cu_launch_kernel = |
10882
6a2d65cb5d7d
fix: gpu_ptx.cpp did not compile on windows
Christian Wirth <christian.wirth@oracle.com>
parents:
10879
diff
changeset
|
305 CAST_TO_FN_PTR(cuda_cu_launch_kernel_func_t, os::dll_lookup(handle, "cuLaunchKernel")); |
10879 | 306 if (TraceGPUInteraction) { |
307 tty->print_cr("[CUDA] Success: library linkage"); | |
308 } | |
309 return true; | |
310 } else { | |
311 // Unable to dlopen libcuda | |
312 return false; | |
313 } | |
314 } else { | |
315 tty->print_cr("Unsupported CUDA platform"); | |
316 return false; | |
317 } | |
318 tty->print_cr("Failed to find CUDA linkage"); | |
319 return false; | |
320 } | |
321 |