-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest0_ht.c
More file actions
278 lines (225 loc) · 8.4 KB
/
test0_ht.c
File metadata and controls
278 lines (225 loc) · 8.4 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#define _GNU_SOURCE
#include <algorithms.h>
#include <gen_utils.h> // for randomize array int
#include <gen_data.h> // get_vector, gen_vector, gen_block_tensor
#include <file_utils.h> // for save_to_file
#include <test.h> // for inline functions
#include <rand_utils.h>
#include <stdlib.h> // for free
#include <string.h>
#include <assert.h>
// HT requirements
#include <tensorlibthreads.h>
#include <pthread.h>
#include <unistd.h> // for _SC_NPROCESSORS_ONLN
#define TOTAL_RUNS 20
int test0_ht(int argc, char ** argv) {
// printf("parent: begin\n");
// get the number of threads on CPU
const long num_threads = sysconf( _SC_NPROCESSORS_ONLN );
int rc;
cpu_set_t mask;
pthread_attr_t attr; // Initialise pthread attribute object
pthread_t producer_thread;
///////////////////// SETUP HT PROPERLY HERE
CPU_ZERO( &mask ); // Clears set so that it contains no CPUs
CPU_SET( 0, &mask ); // Set the mask to 0
// Set affinity of the current thread to mask (0)
// The connection is like this: mask -> affinity -> attribute -> thread
rc = pthread_setaffinity_np( pthread_self(), sizeof(cpu_set_t), &mask );
if( rc != 0 ) {
fprintf( stderr, "Error during setting of the consumer thread affinity.\n" );
return 1;
}
///////////////////// SETUP THREAD ATTRIBUTES
CPU_ZERO( &mask );
CPU_SET(num_threads/2, &mask);
rc = pthread_attr_init( &attr );
if( rc != 0 ) {
fprintf( stderr, "Could not initialise pthread attributes.\n" );
return 2;
}
// Set affinity of the process to &mask in attribute
rc = pthread_attr_setaffinity_np( &attr, sizeof(cpu_set_t), &mask );
if( rc != 0 ) {
fprintf( stderr, "Error during setting of affinity.\n" );
return 3;
}
///////////////////// START THREAD HERE
buffer_t buffer = {
.monitor_on_main = PTHREAD_MUTEX_INITIALIZER,
.monitor_begin = PTHREAD_MUTEX_INITIALIZER,
.monitor_end = PTHREAD_MUTEX_INITIALIZER,
.steady_state = PTHREAD_COND_INITIALIZER,
.preface = PTHREAD_COND_INITIALIZER,
.tensor = NULL,
.unfold_1 = NULL,
.unfold_2 = NULL,
.mode = -1
}; // or below: other init method
// Initializes a mutex
// if (pthread_mutex_init(&data.lock1, NULL) != 0) {
// printf("\n mutex init failed\n");
// return 6;
// }
// set the monitor_on_main
mythread_mutex_lock(&buffer.monitor_on_main);
// Create a thread according to that &attr and with that &data object
mythread_create(&producer_thread, &attr, tvm_block_major_input_aligned_output_aligned_BLAS_POWERS_unfold_mine_nontemporal_producer, (void*)&buffer);
///////////////////// START TESTING CODE HERE
/////////////////////
/////////////////////
/////////////////////
/////////////////////
/////////////////////
/////////////////////
int dim_min, dim_max, n_min, n_max;
int mode_min, mode_max;
int block_n_min, block_n_max;
// we must provide default arguments
dim_min = 4;
dim_max = 4;
mode_min = 0;
mode_max = dim_max-1;
n_min = 1;
n_max = 64;
block_n_min = 1;
block_n_max = n_max;
// if an odd number:
// -> the last element is the specific value for block_n
// block_n = argv-1 (last element)
if ((argc % 2) != 0) {
//printf("block_n=%s\n", *(argv+argc--));
// CONVERT string representation to integer
sscanf (*(argv+argc), "%d", &block_n_min);
sscanf (*(argv+argc), "%d", &block_n_max);
argc--;
// we did -- to decrease used argument count (to say we used this el)
}
switch (argc) {
case 6:
// mode
sscanf (*(argv+argc--), "%d", &mode_max);
sscanf (*(argv+argc--), "%d", &mode_min);
printf("int mode_min=%d\n", mode_min);
printf("int mode_max=%d\n", mode_max);
case 4:
// dim, n
sscanf (*(argv+argc--), "%d", &n_max);
sscanf (*(argv+argc--), "%d", &n_min);
sscanf (*(argv+argc--), "%d", &dim_max);
sscanf (*(argv+argc--), "%d", &dim_min);
}
if (dim_max != 3) {
mode_max = dim_max - 1;
}
printf("int dim_min=%d\n", dim_min);
printf("int dim_max=%d\n", dim_max);
printf("int n_min=%d\n", n_min);
printf("int n_max=%d\n", n_max);
printf("int mode_min=%d\n", mode_min);
printf("int block_n_min=%d\n", block_n_min);
printf("int block_n_max=%d\n", block_n_max);
printf("int mode_max=%d\n", mode_max);
char filename[BUFSIZE];
char filename2[BUFSIZE];
typedef void (*TVM)();
// PROBLEM: we don't have this algorithm in external code (yet)
TVM model_algorithm = tvm_tensor_major;
TVM unfold_unfold_algorithms[] = {
// tvm_block_major_input_aligned_output_aligned_BLAS_POWERS_unfold_mine_nontemporal_consumer1,
tvm_block_major_input_aligned_output_aligned_BLAS_POWERS_unfold_mine_nontemporal_consumer,
// tvm_block_major_input_aligned_output_aligned_BLAS_POWERS_unfold_mine_nontemporal
};
int count[] = {
1
};
for (int runs=1; runs<TOTAL_RUNS; ++runs) {
printf("runs=%d\n", runs);
for (size_t dim=(size_t) dim_min; dim<=(size_t) dim_max; ++dim) {
printf("dim=%zu:\n", dim);
size_t block_layout[dim];
size_t tensor_layout[dim];
for (size_t mode=mode_min; mode <= (size_t) mode_max; ++mode) {
printf(" mode=%zu:\n", mode);
randomize_array_int(block_layout, dim, rand_int(1,rand_int(runs, runs*3)));
printf(" block_layout = ");
print_to_console_sizet(block_layout, dim);
// make tensor layout a multiple of that
size_t block_size = 1;
size_t mat_size = 1;
for (size_t d=dim-1; d<dim; --d) {
if (d > mode) {
mat_size *= block_layout[d];
}
}
mat_size *= block_layout[mode];
for (size_t d=0; d<dim; ++d) {
tensor_layout[d] = block_layout[d] * (int) rand_int(1,4);
block_size *= block_layout[d];
}
DTYPE * unfold = get_aligned_memory(sizeof(DTYPE) * block_size, ALIGNMENT_BLOCK);
memset(unfold, 0, block_size);
DTYPE * unfold_2 = get_aligned_memory(sizeof(DTYPE) * block_size, ALIGNMENT_BLOCK);
memset(unfold_2, 0, block_size);
printf(" tensor_layout = ");
print_to_console_sizet(tensor_layout, dim);
struct tensor_storage *tensor = gen_block_tensor(dim, tensor_layout, block_layout);
struct lin_storage *vector = gen_vector(tensor->layout[mode]);
/////////////////////
struct tensor_storage *result = get_block_result_tensor(tensor, mode);
struct tensor_storage *model_result = get_block_result_tensor(tensor, mode);
// Perform the model algorithm TMV
model_algorithm(tensor, vector, &model_result->lin, mode);
int out_algo = -1;
int algo_counter = 0;
size_t n = 0;
size_t block_n = 0;
//////////////////////////////////////////////////////////////////// BLOCK
// struct tensor_storage *blocked_tensor = get_block_tensor(tensor, 0, 0);
// struct tensor_storage *unblocked_result = get_block_tensor(model_result, 0, 0);
struct tensor_storage *morton_blocked_tensor = get_block_tensor(tensor, 0, 1);
struct tensor_storage *morton_unblocked_result = get_block_tensor(model_result, 0, 1);
// Operation on shared memory: tensor, vector pointers are copied over (?)
// buffer.tensor = blocked_tensor;
buffer.tensor = morton_blocked_tensor;
buffer.unfold_1 = unfold;
buffer.unfold_2 = unfold_2;
buffer.mode = mode;
// if (mode == dim-1) {
out_algo = test_algorithms(unfold_unfold_algorithms, algo_counter, count[0], &result->lin, morton_unblocked_result->lin.data, morton_blocked_tensor, vector, mode,
filename, filename2, dim, n, block_n, out_algo, NULL, &buffer, NULL);
// }
algo_counter += count[0];
// if (mode == dim-1) {
// out_algo = test_algorithms(unfold_unfold_algorithms, algo_counter, count[1], &result->lin, model_result->lin.data, morton_blocked_tensor, vector, mode,
// filename, filename2, dim, n, block_n, out_algo, unfold, NULL, NULL);
// // }
// algo_counter += count[1];
// free_tensor_storage(unblocked_result);
// free_tensor_storage(blocked_tensor);
free_tensor_storage(morton_unblocked_result);
free_tensor_storage(morton_blocked_tensor);
////////////////////////////////////////////////////////////////////
if (out_algo != -1 && DUMP) {
snprintf(filename, BUFSIZE, "%zu %zu %d", dim, mode, -1);
SAVE(model_result->lin);
}
free(unfold);
free(unfold_2);
free_tensor_storage(tensor);
free_tensor_storage(result);
free_tensor_storage(model_result);
free_lin_storage(vector);
}
}
}
// Prepare for closure: first set tensor to NULL then unlock so producer can realise it's time to finish(!)
buffer.tensor = NULL;
mythread_mutex_unlock(&buffer.monitor_on_main);
mythread_join(producer_thread, NULL);
// printf( "Joining producer thread...\n" );
// printf("parent: end\n");
pthread_attr_destroy(&attr);
return 0;
}