-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
210 lines (154 loc) · 5.38 KB
/
main.cpp
File metadata and controls
210 lines (154 loc) · 5.38 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
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <unistd.h>
#include <memory>
#include <cstring>
#include <vector>
#include <x86intrin.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <fcntl.h>
#include <inttypes.h>
#include <linux/aio_abi.h>
#include <cmath>
//#ifndef __rdtsc
//unsigned long long __rdtsc()
//{
//return 1;
//}
//#endif
void mygemm()
{
const int n = 100000;
int input[n];
}
/// This function generated temporary file, opens it
// and fill with random integers then close it
int generate_data(char* filename_template, const unsigned int size_of_file, int num_chunks,int chunk_size,int num_elements_in_chunk )
{
int fd = mkstemp(filename_template);
if (fd == -1) {
std::cerr << "Error opening/creating temporary file" << std::endl;
return(-1);
}
std::cout << "Generating File of random numbers of size: " << size_of_file << " ..."<< std::endl;
std::unique_ptr<int[]> bufek(new int[num_elements_in_chunk]);
for (unsigned int i=0; i < num_chunks; ++i) {
for (unsigned int j=0; j< num_elements_in_chunk;++j) {
(bufek.get())[j] = i* j % 1024;
}
write(fd, bufek.get(), chunk_size);
}
sync();
close(fd);
std::cout << "..done" << std::endl;
return fd;
}
void cleanup(char * filename_template)
{
unlink(filename_template);
}
unsigned long long synchronous_read(char* filename_template, unsigned int size_of_file, int num_chunks,int chunk_size,int num_elements_in_chunk )
{
int fd = open(filename_template,O_RDONLY);
// Read Chunk , calculate hash (sumish)
std::unique_ptr<int[]> bufek(new int[num_elements_in_chunk]);
long long int sumish = 0;
auto t0 = __rdtsc();
for(unsigned int i=0; i<num_chunks;++i) {
read(fd, bufek.get(), chunk_size);
for(unsigned int j=0; j< num_elements_in_chunk;++j) {
sumish += pow((bufek.get())[j],2) + 3;
}
}
auto t1 = __rdtsc();
close(fd);
std::cout << "SYNC sumish=" << sumish << std::endl;
return (t1 - t0);
}
unsigned long long asynchronous_read(char* filename_template, unsigned int size_of_file, int num_chunks,int chunk_size,int num_elements_in_chunk )
{
aio_context_t ctx;
struct iocb cb;
struct iocb *cbs[1];
struct io_event events[1];
int ret;
ctx = 0; // This is requested by io_setup
ret = syscall(__NR_io_setup, 128, &ctx); // I had 2, but 128 was in tutorial
if (ret < 0 ) {
std::cerr << "Error creatin AIO context" << std::endl;
return 0;
}
int fd = open(filename_template,O_RDONLY);
// Read Chunk , calculate hash (sumish)
std::unique_ptr<int[]> buf1(new int[num_elements_in_chunk]);
std::unique_ptr<int[]> buf2(new int[num_elements_in_chunk]);
// Queue with pointers
// we will have a double buffer here eg.
// read to one buffer and process another one
std::vector<int*> buffers;
buffers.push_back(buf1.get());
buffers.push_back(buf2.get());
long long int sumish = 0;
int index = 0;
auto t0 = __rdtsc();
read(fd, buffers[index], chunk_size);
index = 1 - index; // Get next element (0 -> 1) or first one if we processed last one ( 1 -> 0)
for(unsigned int i=0; i<num_chunks;++i) {
// Initiate AIO read to buffers[index]
memset(&cb,0,sizeof(cb));
cb.aio_fildes = fd;
cb.aio_lio_opcode = IOCB_CMD_PREAD;
cb.aio_buf = (uint64_t)(buffers[index]);
cb.aio_buf = (__u64)(buffers[index]);
cb.aio_offset = (i+1)*chunk_size;
cb.aio_nbytes = chunk_size;
cbs[0] = &cb;
ret = syscall(__NR_io_submit, ctx, 1, cbs);
if (ret != 1) {
if(ret < 0) {
perror("Error: Submission of AIO failed!");
}
return 0;
}
for(unsigned int j=0; j< num_elements_in_chunk;++j) {
sumish += pow((buffers[1 - index])[j],2) + 3;
}
// Wait to see if data was read
// wait indefinetly for one event
ret = syscall(__NR_io_getevents,ctx, 1, 1, events, NULL);
if (ret != 1) {
if( ret < 0) {
std::cerr << "Error: Events recieving of AIO failed!" << std::endl;
return 0;
}
}
index = 1 - index; // Get next element (0 -> 1) or first one if we processed last one ( 1 -> 0)
}
auto t1 = __rdtsc();
ret = syscall(__NR_io_destroy, ctx);
if (ret < 0 ) {
std::cerr << "Error destroying AIO context" << std::endl;
return 0;
}
close(fd);
std::cout << "ASYNC sumish=" << sumish << std::endl;
return (t1 - t0);
}
int main()
{
char filename_template[] = "/tmp/AIOXXXXXX"; //< template from which we will have temporary file name generated
//< last six charace4trs need to be "X"
const unsigned int size_of_file = 1024*1024*1024;
int num_chunks = 1024;
int chunk_size = size_of_file / num_chunks;
int num_elements_in_chunk = chunk_size / sizeof(int);
int fd = generate_data(filename_template,size_of_file,num_chunks,chunk_size,num_elements_in_chunk);
auto asyn_time = asynchronous_read(filename_template, size_of_file,num_chunks,chunk_size,num_elements_in_chunk);
auto syn_time = synchronous_read(filename_template, size_of_file,num_chunks,chunk_size,num_elements_in_chunk);
const unsigned long tsc_freq = 2000000000;
std::cout << "Synchronous Read and Compute: " << 1000.0f*syn_time/(float)tsc_freq << " ms ("<< syn_time << ")"<< std::endl;
std::cout << "Asynchronous Read and Compute: " << 1000.0f*asyn_time/(float)tsc_freq << " ms ("<< asyn_time << ")"<< std::endl;
cleanup(filename_template);
}