Mercurial > hg > graal-compiler
comparison src/gpu/ptx/gpu_ptx.cpp @ 9430:147162b27799
GRAAL-234 - PTX code loading
author | Morris Meyer <morris.meyer@oracle.com> |
---|---|
date | Tue, 30 Apr 2013 08:17:55 -0400 |
parents | |
children | 62838eadbf56 |
comparison
equal
deleted
inserted
replaced
9429:aaf8798b0969 | 9430:147162b27799 |
---|---|
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 | |
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_launch_kernel_func_t gpu::Ptx::_cuda_cu_launch_kernel; | |
41 gpu::Ptx::cuda_cu_module_get_function_func_t gpu::Ptx::_cuda_cu_module_get_function; | |
42 gpu::Ptx::cuda_cu_module_load_data_ex_func_t gpu::Ptx::_cuda_cu_module_load_data_ex; | |
43 | |
44 void gpu::probe_linkage() { | |
45 #ifdef __APPLE__ | |
46 set_gpu_linkage(gpu::Ptx::probe_linkage_apple()); | |
47 #else | |
48 set_gpu_linkage(false); | |
49 #endif | |
50 } | |
51 | |
52 void gpu::initialize_gpu() { | |
53 if (gpu::has_gpu_linkage()) { | |
54 set_initialized(gpu::Ptx::initialize_gpu()); | |
55 } | |
56 } | |
57 | |
58 void gpu::generate_kernel(unsigned char *code, int code_len, const char *name) { | |
59 if (gpu::has_gpu_linkage()) { | |
60 gpu::Ptx::generate_kernel(code, code_len, name); | |
61 } | |
62 } | |
63 | |
64 #define __CUDA_API_VERSION 5000 | |
65 | |
66 bool gpu::Ptx::initialize_gpu() { | |
67 int status = _cuda_cu_init(0, __CUDA_API_VERSION); | |
68 if (TraceWarpLoading) { | |
69 tty->print_cr("gpu_ptx::_cuda_cu_init: %d", status); | |
70 } | |
71 | |
72 int device_count = 0; | |
73 status = _cuda_cu_device_get_count(&device_count); | |
74 if (TraceWarpLoading) { | |
75 tty->print_cr("gpu_ptx::_cuda_cu_device_get_count(%d): %d", device_count, status); | |
76 } | |
77 | |
78 int device_id = 0, cu_device = 0; | |
79 status = _cuda_cu_device_get(&cu_device, device_id); | |
80 if (TraceWarpLoading) { | |
81 tty->print_cr("gpu_ptx::_cuda_cu_device_get(%d): %d", cu_device, status); | |
82 } | |
83 | |
84 int major, minor; | |
85 status = _cuda_cu_device_compute_capability(&major, &minor, cu_device); | |
86 if (TraceWarpLoading) { | |
87 tty->print_cr("gpu_ptx::_cuda_cu_device_compute_capability(major %d, minor %d): %d", | |
88 major, minor, status); | |
89 } | |
90 | |
91 char device_name[256]; | |
92 status = _cuda_cu_device_get_name(device_name, 256, cu_device); | |
93 if (TraceWarpLoading) { | |
94 tty->print_cr("gpu_ptx::_cuda_cu_device_get_name(%s): %d", device_name, status); | |
95 } | |
96 | |
97 status = _cuda_cu_ctx_create(&_device_context, 0, cu_device); | |
98 if (TraceWarpLoading) { | |
99 tty->print_cr("gpu_ptx::_cuda_cu_ctx_create(%x): %d", _device_context, status); | |
100 } | |
101 | |
102 return status == 0; // CUDA_SUCCESS | |
103 } | |
104 | |
105 void gpu::Ptx::generate_kernel(unsigned char *code, int code_len, const char *name) { | |
106 | |
107 void *cu_module; | |
108 const unsigned int jit_num_options = 3; | |
109 int *jit_options = new int[jit_num_options]; | |
110 void **jit_option_values = new void *[jit_num_options]; | |
111 | |
112 jit_options[0] = 4; // CU_JIT_INFO_LOG_BUFFER_SIZE_BYTES | |
113 int jit_log_buffer_size = 1024; | |
114 jit_option_values[0] = (void *)(size_t)jit_log_buffer_size; | |
115 | |
116 jit_options[1] = 3; // CU_JIT_INFO_LOG_BUFFER | |
117 char *jit_log_buffer = new char[jit_log_buffer_size]; | |
118 jit_option_values[1] = jit_log_buffer; | |
119 | |
120 jit_options[2] = 0; // CU_JIT_MAX_REGISTERS | |
121 int jit_register_count = 32; | |
122 jit_option_values[2] = (void *)(size_t)jit_register_count; | |
123 | |
124 int status = _cuda_cu_module_load_data_ex(&cu_module, code, | |
125 jit_num_options, jit_options, (void **)jit_option_values); | |
126 if (TraceWarpLoading) { | |
127 tty->print_cr("gpu_ptx::_cuda_cu_module_load_data_ex(%x): %d", cu_module, status); | |
128 tty->print_cr("gpu_ptx::jit_log_buffer\n%s", jit_log_buffer); | |
129 } | |
130 | |
131 void *cu_function; | |
132 | |
133 status = _cuda_cu_module_get_function(&cu_function, cu_module, name); | |
134 if (TraceWarpLoading) { | |
135 tty->print_cr("gpu_ptx::_cuda_cu_module_get_function(%s):%x %d", name, cu_function, status); | |
136 } | |
137 } | |
138 | |
139 | |
140 #ifdef __APPLE__ | |
141 bool gpu::Ptx::probe_linkage_apple() { | |
142 void *handle = dlopen("/usr/local/cuda/lib/libcuda.dylib", RTLD_LAZY); | |
143 if (handle != NULL) { | |
144 _cuda_cu_init = | |
145 CAST_TO_FN_PTR(cuda_cu_init_func_t, dlsym(handle, "cuInit")); | |
146 _cuda_cu_ctx_create = | |
147 CAST_TO_FN_PTR(cuda_cu_ctx_create_func_t, dlsym(handle, "cuCtxCreate")); | |
148 _cuda_cu_ctx_detach = | |
149 CAST_TO_FN_PTR(cuda_cu_ctx_detach_func_t, dlsym(handle, "cuCtxDetach")); | |
150 _cuda_cu_ctx_synchronize = | |
151 CAST_TO_FN_PTR(cuda_cu_ctx_synchronize_func_t, dlsym(handle, "cuCtxSynchronize")); | |
152 _cuda_cu_device_get_count = | |
153 CAST_TO_FN_PTR(cuda_cu_device_get_count_func_t, dlsym(handle, "cuDeviceGetCount")); | |
154 _cuda_cu_device_get_name = | |
155 CAST_TO_FN_PTR(cuda_cu_device_get_name_func_t, dlsym(handle, "cuDeviceGetName")); | |
156 _cuda_cu_device_get = | |
157 CAST_TO_FN_PTR(cuda_cu_device_get_func_t, dlsym(handle, "cuDeviceGet")); | |
158 _cuda_cu_device_compute_capability = | |
159 CAST_TO_FN_PTR(cuda_cu_device_compute_capability_func_t, dlsym(handle, "cuDeviceComputeCapability")); | |
160 _cuda_cu_module_get_function = | |
161 CAST_TO_FN_PTR(cuda_cu_module_get_function_func_t, dlsym(handle, "cuModuleGetFunction")); | |
162 _cuda_cu_module_load_data_ex = | |
163 CAST_TO_FN_PTR(cuda_cu_module_load_data_ex_func_t, dlsym(handle, "cuModuleLoadDataEx")); | |
164 return true; | |
165 } | |
166 return false; | |
167 } | |
168 #endif |