-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-write-hole.c
111 lines (100 loc) · 2.93 KB
/
test-write-hole.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* Copyright 2018 Red Hat Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Authors: Jérôme Glisse <[email protected]>
*/
#include "helpers.h"
#define NWORDS (1 << 16)
int main(int argc, char* argv[])
{
enum status status = SUCCESS;
struct cl_program clprog;
char *append = "\n";
int res, fd;
unsigned i;
void *map;
ssize_t len;
/* Create file and initialize its content. */
fd = open("/tmp/." __FILE__, O_CREAT | O_TRUNC | O_RDWR, S_IRWXU);
if (fd < 0) {
append = "creating file failed\n";
status = ERROR;
goto out;
}
for (i = 0; i < NWORDS; ++i) {
write(fd, &i, sizeof(int));
}
fsync(fd);
map = mem_file_map_share(fd, NWORDS * sizeof(int));
if (map == NULL) {
append = "mapping file failed\n";
status = ERROR;
goto out;
}
res = cl_program_init(&clprog, NWORDS);
if (res) {
append = "cl program init failed\n";
status = ERROR;
goto out;
}
/* Check that the GPU can read the mmap file. */
res = cl_program_run(&clprog, map, NULL, NULL);
if (res) {
append = "cl program run failed\n";
status = ERROR;
goto out;
}
if (madvise(map, (NWORDS / 2) * sizeof(unsigned), MADV_REMOVE)) {
append = "madvise(MADV_REMOVE) failed\n";
status = ERROR;
goto out;
}
/* Check that the GPU can read the updated mmap file. */
res = cl_program_run_nocheck(&clprog, map, NULL, NULL);
if (res) {
append = "cl program run failed\n";
status = ERROR;
goto out;
}
for (i = 0; i < NWORDS; ++i) {
if ((i < NWORDS / 2 && clprog.r[i] != -i) ||
(i >= NWORDS / 2 && clprog.r[i] != 0)) {
append = "checking file failed\n";
status = ERROR;
goto out;
}
}
/* Close file, and re-open it and check its content. */
mem_unmap(map, NWORDS * sizeof(int));
close(fd);
fd = open("/tmp/." __FILE__, O_RDWR);
if (fd < 0) {
append = "opening file failed\n";
status = ERROR;
goto out;
}
for (i = 0; i < NWORDS; ++i) {
unsigned v;
len = read(fd, &v, sizeof(int));
if (len != sizeof(v) ||
(i < NWORDS / 2 && v != 0) ||
(i >= NWORDS / 2 && v != i)) {
append = "checking file failed\n";
status = ERROR;
goto out;
}
}
out:
print_status(status, argv, append);
return 0;
}