-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix_mul_demo.cu
95 lines (74 loc) · 2.2 KB
/
matrix_mul_demo.cu
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
#include <stdio.h>
#include <unistd.h>
#include "MatMul.h"
#include "matrix.h"
#define ROW 1024
#define COL 1024
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
bool finish = false;
typedef MatrixIn<int> matrix_in;
typedef MatrixOut<int> matrix_out;
typedef WFThreadTask<matrix_in, matrix_out> MatMulTask;
void check_sync_result(MatMulTask *task)
{
fprintf(stderr, "checking in ThreadTask callback.\n");
matrix_in *in = task->get_input();
matrix_out *out = task->get_output();
matrix_check(in, out);
pthread_mutex_lock(&mutex);
finish = true;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
int main()
{
dim3 thread_per_block(16, 16);
dim3 block_num((COL + thread_per_block.x - 1) / thread_per_block.x,
(ROW + thread_per_block.y - 1) / thread_per_block.y);
matrix_in *in;
matrix_out *out;
// test thread sync task
MatMulTask *sync_task = CudaTaskFactory::create_matmul_task<matrix_in, matrix_out>
(block_num, thread_per_block, check_sync_result);
in = sync_task->get_input();
out = sync_task->get_output();
in->init(ROW, COL);
out->init(ROW, COL);
in->a = (int *)malloc(sizeof(int) * ROW * COL);
in->b = (int *)malloc(sizeof(int) * ROW * COL);
out->c = (int *)malloc(sizeof(int) * ROW * COL);
init_random(in);
fprintf(stderr, "start sync thread task\n");
sync_task->start();
pthread_mutex_lock(&mutex);
while (!finish)
pthread_cond_wait(&cond, &mutex);
pthread_mutex_unlock(&mutex);
free(in->a);
free(in->b);
free(out->c);
/*
// test async task
MatMulAsyncTask *async_task = CUDATaskFactory::create_matmul_async_task<MatrixIn, MatrixOut>
(block_num, thread_per_block, check_async_result);
in = async_task->get_input();
out = async_task->get_output();
in->init(ROW, COL);
out->init(ROW, COL);
in->a = (int *)malloc(sizeof(int) * ROW * COL);
in->b = (int *)malloc(sizeof(int) * ROW * COL);
out->c = (int *)malloc(sizeof(int) * ROW * COL);
init_random(in);
fprintf(stderr, "start async request task\n");
async_task->start();
pthread_mutex_lock(&mutex);
while (!finish)
pthread_cond_wait(&cond, &mutex);
pthread_mutex_unlock(&mutex);
free(in->a);
free(in->b);
free(out->c);
*/
return 0;
}