-
Notifications
You must be signed in to change notification settings - Fork 118
/
sync_transfer.c
93 lines (74 loc) · 2.06 KB
/
sync_transfer.c
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
#include <stdio.h>
#include <stdlib.h>
#define __ONMIC__ __attribute__((target(mic)))
/**
* 异步传输数据 offload_transfer
*/
__ONMIC__ void add_inputs(int n, float *f1, float *f2){
int i;
for( i =0; i < n; i++) {
f2[i] += f1[i];
}
}
void display_vals( int id, int n, float *f2) {
printf("\nResults after Offload #%d:\n",id);
int i;
for ( i = 0; i < n; i++) {
printf("f2[%d] is %f\n", i, f2[i] );
}
printf("====================\n");
}
void test() {
float *f1 , *f2;
int n = 10000;
int i, j;
f1 = (float*) malloc(sizeof(float) * n);
f2 = (float*)malloc(sizeof(float) * n);
for(i = 0; i < n; i++) {
f1[i] = i+1;
f2[i] = 0.0;
}
// 这里只上传数据
#pragma offload_transfer target(mic:0) signal(f1) \
in (f1:length(n) alloc_if(1) free_if(0))\
in (f2:length(n) alloc_if(1) free_if(0))
// wait(f1)等待上面的数据传输完毕, 再执行该操作
#pragma offload target(mic:0) wait(f1) signal(f2) \
in(n) \
nocopy(f1:alloc_if (0) free_if(1))\
out(f2:length(n) alloc_if(0) free_if(1))
add_inputs(n, f1, f2);
// 等该f2执行完
#pragma offload_wait target(mic:0) wait(f2)
// 如果不加wait, 就会全部打印出0
display_vals(1, 10, f2);
// 多个数据异步上传
#pragma offload_transfer target(mic:0) signal(f1) \
in(f1:length(n) alloc_if(1) free_if(0))
#pragma offload_transfer target(mic:0) signal(f2) \
in(f2:length(n) alloc_if(1) free_if(0))
// 同时等待两个信号量
#pragma offload target(mic:0) wait(f1, f2) \
in (n) \
nocopy (f1:alloc_if(0) free_if(1)) \
out (f2:length(n) alloc_if(0) free_if(1))
add_inputs(n, f1, f2);
display_vals(2, 10, f2);
// 异步传输和同步传输结合
#pragma offload_transfer target(mic:0) signal(f2)\
in(f2:length(n) alloc_if(1) free_if(0))
#pragma offload target(mic:0) wait(f2) \
in(n) \
in(f1:length(n) alloc_if(1) free_if(0))\
nocopy(f2)
add_inputs(n ,f1, f2);
#pragma offload_transfer target(mic:0) signal(f2) \
out(f2:length(n) alloc_if(0) free_if(1))
#pragma offload_wait target(mic:0) wait(f2)
display_vals(3, 10, f2);
free(f1);
free(f2);
}
int main() {
test();
}