This repository was archived by the owner on Dec 16, 2022. It is now read-only.
forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathj2c-calls.cpp
215 lines (190 loc) · 8.19 KB
/
j2c-calls.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* Copyright (c) 2014 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
static char* for_loops_begin(ast_node_result_t* result, size_t from_level, size_t to_level, jl_codectx_t *ctx, bool generate_omp = true)
{
assert(from_level <= to_level);
// You must print something to the code buffer, even an empty string, since the caller
// expects this behavior, and might have ajdusted c_code_end to point to '\0' in order
// to connect with the new string here.
char*start;
NEW_C_CODE_2(start, "");
if (from_level == to_level) {
return start;
}
char* str, iv;
size_t index, id;
if (from_level == 0) {
// TODO: make it general. This only for trying offload with the vector add test case.
// NEW_C_CODE_2(start, "_Offload_status x;\nOFFLOAD_STATUS_INIT(x);\n");
// NEW_C_CODE_2(start, "#pragma offload target(mic) status(x) optional in(a,b,c: length(N1*N2) alloc_if(a) free_if(1)) out(d:length(N1*N2) alloc_if(a) free_if(1))\n");
if (generate_omp) {
CONTINUE_C_CODE_2(str, "#pragma offload target(mic)\n");
CONTINUE_C_CODE_2(str, "{\n");
CONTINUE_C_CODE_2(str, "ArenaManager *am = (ArenaManager*)malloc(sizeof(ArenaManager));\n");
CONTINUE_C_CODE_2(str, "ArenaManager_ctor(am,100000000,65536,240);\n");
CONTINUE_C_CODE_2(str, "#pragma omp parallel\n");
CONTINUE_C_CODE_2(str, "{\n");
CONTINUE_C_CODE_2(str, "ArenaClient *ac;\n");
CONTINUE_C_CODE_2(str, "prepare_alloc(am,&ac);\n");
CONTINUE_C_CODE_2(str, "#pragma omp for\n");
}
}
if (from_level == to_level - 1) {
CONTINUE_C_CODE_2(str, "#pragma vector always\n");
}
iv = 'i' + from_level;
PREPARE_TO_CONTINUE_C_CODE;
NEW_C_CODE_7(str, "for (int64_t __%c=0; __%c < %s.N%d; __%c++) {\n", iv, iv, result_variable_string(result), from_level, iv);
for (size_t i = from_level + 1; i < to_level; i++) {
iv = 'i' + i;
if (i == to_level - 1) {
PREPARE_TO_CONTINUE_C_CODE;
NEW_C_CODE_2(str, "#pragma vector always\n");
}
PREPARE_TO_CONTINUE_C_CODE;
NEW_C_CODE_7(str, "for (int64_t __%c=0; __%c < %s.N%d; __%c++) {\n", iv, iv, result_variable_string(result), i, iv);
}
return start;
}
static char* for_loops_end(size_t from_level, size_t to_level, bool generate_omp = true)
{
char *str;
assert(from_level <= to_level);
char*start;
if (from_level == to_level) {
// You must print something to the code buffer, even an empty string, since the caller
// expects this behavior, and might have ajdusted c_code_end to point to '\0' in order
// to connect with the new string here.
NEW_C_CODE_2(start, "");
return start;
}
NEW_C_CODE_2(start, "}\n");
for (size_t i = from_level + 1; i < to_level; i++) {
char* str;
PREPARE_TO_CONTINUE_C_CODE;
NEW_C_CODE_2(str, "}\n");
}
if (from_level == 0) {
if (generate_omp) {
CONTINUE_C_CODE_2(str, "finish_alloc(ac);\n");
CONTINUE_C_CODE_2(str, "}\n"); // end of #pragma omp parallel section
CONTINUE_C_CODE_2(str, "ArenaManager_dtor(am);\n");
CONTINUE_C_CODE_2(str, "}\n"); // end of #pragma offload section
}
}
return start;
}
static inline void add_aliased_arrays(ast_node_result_t* l, ast_node_result_t* r)
{
// Both input should be arrays, or not arrays.
assert(!jl_is_array_type(l->result_var_type) || jl_is_array_type(r->result_var_type));
assert(!jl_is_array_type(r->result_var_type) || jl_is_array_type(l->result_var_type));
if (jl_is_array_type(r->result_var_type)) {
std::pair<jl_value_t*, jl_value_t*> p;
p.first = l->result_var;
p.second = r->result_var;
cur_func_result->aliased_arrays.push_back(p);
#ifdef J2C_VERBOSE
if (enable_j2c_verbose) {
JL_PRINTF(JL_STDOUT, "\nAliased arrays:");
jl_static_show(JL_STDOUT, p.first);
jl_static_show(JL_STDOUT, p.second);
}
#endif
}
}
static void j2c_copy(ast_node_result_t* result, ast_node_result_t* l, ast_node_result_t* r, jl_codectx_t *ctx)
{
if (l->result_var == r->result_var) {
return;
}
add_aliased_arrays(l, r);
#ifdef MIN_TEMPS
if (l->is_temporary) {
l->representative = r;
return;
}
// Non-temporary (user var) still needs explicit assigment.
#endif
if (!jl_is_array_type(l->result_var_type) || result->num_dims_fused == 0) {
NEW_C_CODE_4(result->epilog, "%s = %s;\n", result_variable_string(l), result_variable_string(r));
return;
}
size_t num_dims_fused = result->num_dims_fused;
/* if (num_dims_fused > 0) {
size_t from_level = (result->parent != NULL) ? result->parent->num_dims_fused : 0;
size_t to_level = result->result_var_dims.size();
result->prolog = for_loops_begin(result, from_level, to_level, ctx);
NEW_C_CODE_4(result->epilog, "%s = %s;\n", array_element(l, ctx), array_element(r, ctx));
PREPARE_TO_CONTINUE_C_CODE;
for_loops_end(from_level, to_level);
return;
}
*/
// TOFIX: check the type of the return array, and decide the character code
NEW_C_CODE_5(result->epilog, "cblas_dcopy(%s.len(), %s.data, 1, %s.data, 1);\n", result_variable_string(r),
result_variable_string(r), result_variable_string(l));
}
static void j2c_add(ast_node_result_t* result, std::vector<ast_node_result_t*>& args_results, jl_codectx_t *ctx)
{
char* str;
/* if (result->result_var_dims.size() == 0) { // scalar
NEW_C_CODE_4(result->epilog, "%s = %s", ((jl_sym_t*)(result->result_var))->name,
result_variable_string(args_results[0]));
for (size_t i = 1; i < args_results.size(); i++) {
PREPARE_TO_CONTINUE_C_CODE;
NEW_C_CODE_3(str, "+%s", result_variable_string(args_results[i]));
}
PREPARE_TO_CONTINUE_C_CODE;
NEW_C_CODE_2(str, ";\n");
return;
}
*/
assert(args_results.size() == 2);
size_t num_dims_fused = result->num_dims_fused;
/* if (num_dims_fused > 0) {
size_t from_level = (result->parent != NULL) ? result->parent->num_dims_fused : 0;
size_t to_level = result->result_var_dims.size();
result->prolog = for_loops_begin(result, from_level, to_level, ctx);
NEW_C_CODE_5(result->epilog, "%s = %s + %s;\n", array_element(result, ctx),
array_element(args_results[0], ctx), array_element(args_results[1], ctx));
PREPARE_TO_CONTINUE_C_CODE;
for_loops_end(from_level, to_level);
return;
}
*/
assert(num_dims_fused == 0);
//*axpy is defined as y = a*x + y. So the result and second operand must be the same
if (result->result_var != args_results[1]->result_var) {
j2c_copy(result, result, args_results[1], ctx);
// TOFIX: check the type of the return array, and decide the character code
CONTINUE_C_CODE_3(str, "cblas_daxpy(%s.len()", result_variable_string(result));
CONTINUE_C_CODE_4(str, ", 1.0, %s.data, 1, %s.data, 1);\n", result_variable_string(args_results[0]),
result_variable_string(result));
} else {
// TOFIX: check the type of the return array, and decide the character code
NEW_C_CODE_3(result->epilog, "cblas_daxpy(%s.len()", result_variable_string(result));
CONTINUE_C_CODE_4(str, ", 1.0, %s.data, 1, %s.data, 1);\n", result_variable_string(args_results[0]),
result_variable_string(args_results[1]));
}
}