comparison src/gpu/hsail/vm/gpu_hsail.cpp @ 12743:f1a55428a8d7

more HSAIL support in the C++ layer for executing HSAIL code on the simulator Contributed-by: Eric Caspole <eric.caspole@amd.com>
author Doug Simon <doug.simon@oracle.com>
date Sun, 10 Nov 2013 13:18:09 +0100
parents
children 14db5ffd5ed9
comparison
equal deleted inserted replaced
12742:40924dbc623b 12743:f1a55428a8d7
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/javaCalls.hpp"
27 #include "runtime/gpu.hpp"
28 #include "utilities/globalDefinitions.hpp"
29 #include "utilities/ostream.hpp"
30 #include "memory/allocation.hpp"
31 #include "memory/allocation.inline.hpp"
32 #include "hsailKernelArguments.hpp"
33
34 void * gpu::Hsail::_device_context;
35
36 gpu::Hsail::okra_ctx_create_func_t gpu::Hsail::_okra_ctx_create;
37 gpu::Hsail::okra_kernel_create_func_t gpu::Hsail::_okra_kernel_create;
38 gpu::Hsail::okra_push_object_func_t gpu::Hsail::_okra_push_object;
39 gpu::Hsail::okra_push_boolean_func_t gpu::Hsail::_okra_push_boolean;
40 gpu::Hsail::okra_push_byte_func_t gpu::Hsail::_okra_push_byte;
41 gpu::Hsail::okra_push_double_func_t gpu::Hsail::_okra_push_double;
42 gpu::Hsail::okra_push_float_func_t gpu::Hsail::_okra_push_float;
43 gpu::Hsail::okra_push_int_func_t gpu::Hsail::_okra_push_int;
44 gpu::Hsail::okra_push_long_func_t gpu::Hsail::_okra_push_long;
45 gpu::Hsail::okra_execute_with_range_func_t gpu::Hsail::_okra_execute_with_range;
46 gpu::Hsail::okra_clearargs_func_t gpu::Hsail::_okra_clearargs;
47 gpu::Hsail::okra_register_heap_func_t gpu::Hsail::_okra_register_heap;
48
49
50 bool gpu::Hsail::initialize_gpu() {
51 // All the initialization is done in the okra library so
52 // nothing to do here.
53 if (TraceGPUInteraction) {
54 tty->print_cr("[HSAIL] Simulator: initialize_gpu");
55 }
56 return true;
57 }
58
59 unsigned int gpu::Hsail::total_cores() {
60 // This is not important with simulator
61 return 1;
62 }
63
64 void gpu::Hsail::register_heap() {
65 // After the okra functions are set up and the heap is initialized, register the java heap with HSA
66 guarantee(Universe::heap() != NULL, "heap should be there by now.");
67 if (TraceGPUInteraction) {
68 tty->print_cr("[HSAIL] heap=" PTR_FORMAT, Universe::heap());
69 tty->print_cr("[HSAIL] base=0x%08x, capacity=%ld", Universe::heap()->base(), Universe::heap()->capacity());
70 }
71 _okra_register_heap(Universe::heap()->base(), Universe::heap()->capacity());
72 }
73
74 bool gpu::Hsail::execute_kernel_void_1d(address kernel, int dimX, jobject args, methodHandle& mh) {
75 objArrayOop argsArray = (objArrayOop) JNIHandles::resolve(args);
76
77 // Reset the kernel arguments
78 _okra_clearargs(kernel);
79
80 // This object sets up the kernel arguments
81 HSAILKernelArguments hka(kernel, mh->signature(), argsArray, mh->is_static());
82
83 // Run the kernel
84 bool success = _okra_execute_with_range(kernel, dimX);
85 return success;
86 }
87
88 void *gpu::Hsail::generate_kernel(unsigned char *code, int code_len, const char *name) {
89
90 gpu::Hsail::register_heap();
91
92 // The kernel entrypoint is always run for the time being
93 const char* entryPointName = "&run";
94
95 _device_context = _okra_ctx_create();
96
97 // code is not null terminated, must be a better way to do this
98 unsigned char* nullTerminatedCodeBuffer = (unsigned char*) malloc(code_len + 1);
99 memcpy(nullTerminatedCodeBuffer, code, code_len);
100 nullTerminatedCodeBuffer[code_len] = 0;
101 void* kernel = _okra_kernel_create(_device_context, nullTerminatedCodeBuffer, entryPointName);
102 free(nullTerminatedCodeBuffer);
103 return kernel;
104 }
105
106 #if defined(LINUX)
107 static const char okra_library_name[] = "libokra_x86_64.so";
108 #elif defined (_WINDOWS)
109 static char const okra_library_name[] = "okra_x86_64.dll";
110 #else
111 static char const okra_library_name[] = "";
112 #endif
113
114 #define STD_BUFFER_SIZE 1024
115
116 bool gpu::Hsail::probe_linkage() {
117 if (okra_library_name != NULL) {
118 char *buffer = (char*)malloc(STD_BUFFER_SIZE);
119 if (TraceGPUInteraction) {
120 tty->print_cr("[HSAIL] library is %s", okra_library_name);
121 }
122 void *handle = os::dll_load(okra_library_name, buffer, STD_BUFFER_SIZE);
123 free(buffer);
124 if (handle != NULL) {
125
126 _okra_ctx_create =
127 CAST_TO_FN_PTR(okra_ctx_create_func_t, os::dll_lookup(handle, "okra_create_context"));
128 _okra_kernel_create =
129 CAST_TO_FN_PTR(okra_kernel_create_func_t, os::dll_lookup(handle, "okra_create_kernel"));
130 _okra_push_object =
131 CAST_TO_FN_PTR(okra_push_object_func_t, os::dll_lookup(handle, "okra_push_object"));
132 _okra_push_boolean =
133 CAST_TO_FN_PTR(okra_push_boolean_func_t, os::dll_lookup(handle, "okra_push_boolean"));
134 _okra_push_byte =
135 CAST_TO_FN_PTR(okra_push_byte_func_t, os::dll_lookup(handle, "okra_push_byte"));
136 _okra_push_double =
137 CAST_TO_FN_PTR(okra_push_double_func_t, os::dll_lookup(handle, "okra_push_double"));
138 _okra_push_float =
139 CAST_TO_FN_PTR(okra_push_float_func_t, os::dll_lookup(handle, "okra_push_float"));
140 _okra_push_int =
141 CAST_TO_FN_PTR(okra_push_int_func_t, os::dll_lookup(handle, "okra_push_int"));
142 _okra_push_long =
143 CAST_TO_FN_PTR(okra_push_long_func_t, os::dll_lookup(handle, "okra_push_long"));
144 _okra_execute_with_range =
145 CAST_TO_FN_PTR(okra_execute_with_range_func_t, os::dll_lookup(handle, "okra_execute_with_range"));
146 _okra_clearargs =
147 CAST_TO_FN_PTR(okra_clearargs_func_t, os::dll_lookup(handle, "okra_clearargs"));
148 _okra_register_heap =
149 CAST_TO_FN_PTR(okra_register_heap_func_t, os::dll_lookup(handle, "okra_register_heap"));
150
151 if (TraceGPUInteraction) {
152 tty->print_cr("[HSAIL] Success: library linkage _okra_clearargs=0x%08x", _okra_clearargs);
153 }
154 return true;
155 } else {
156 // Unable to dlopen okra
157 tty->print_cr("[HSAIL] library load failed.");
158 return false;
159 }
160 } else {
161 tty->print_cr("Unsupported HSAIL platform");
162 return false;
163 }
164 tty->print_cr("Failed to find HSAIL linkage");
165 return false;
166 }