-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfast.c
More file actions
127 lines (110 loc) · 3.1 KB
/
fast.c
File metadata and controls
127 lines (110 loc) · 3.1 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
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <math.h>
typedef struct
{
char *filename;
long startOffset;
long endOffset;
long blockSize;
uint threadXor;
} thread_data;
double now()
{
struct timeval tv;
gettimeofday(&tv, 0);
return tv.tv_sec + tv.tv_usec / 1000000.0;
}
long filesize(const char *filename)
{
struct stat st;
stat(filename, &st);
return st.st_size;
}
void *thread_read(void *arg)
{
thread_data *data = (thread_data *)arg;
char *filename = data->filename;
long startOffset = data->startOffset;
long endOffset = data->endOffset;
long blockSize = data->blockSize;
uint threadXOR = 0;
unsigned int *buf = (unsigned int *)malloc(blockSize);
int fd = open(filename, O_RDONLY);
if (fd == -1)
{
printf("File Not Found.\r\n");
pthread_exit(NULL);
}
lseek(fd, startOffset, SEEK_SET);
for (long i = 0; i < endOffset; i += blockSize)
{
size_t bytes_read = read(fd, buf, blockSize);
for (size_t i = 0; i < bytes_read / sizeof(unsigned int); i++)
{
threadXOR ^= buf[i];
}
}
data-> threadXor=threadXOR;
free(buf);
close(fd);
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
return 1;
}
char *filename = argv[1];
long fsize = filesize(argv[1]);
int numThreads = 2;
long blockSize = (long)(6 * pow(fsize, 0.45));
blockSize = blockSize/numThreads;
blockSize = blockSize - blockSize % 4;
long threadFileBlock=(long)fsize/numThreads;
threadFileBlock = threadFileBlock - threadFileBlock % blockSize;
pthread_t threads[numThreads];
thread_data data[numThreads];
double startT = now();
for (int i = 0; i < numThreads; i++)
{
data[i].filename = filename;
data[i].startOffset = (i*threadFileBlock);
if(i==0){
data[i].startOffset = 0;
}
data[i].endOffset=data[i].startOffset+threadFileBlock;
if(i==numThreads-1){
data[i].endOffset=fsize;
}
data[i].blockSize=blockSize;
data[i].threadXor=0;
// printf("\n[%d] Start:%ld End:%ld BlockSize:%ld",data[i].thread_num,data[i].startOffset,data[i].endOffset,data[i].blockSize);
pthread_create(&threads[i], NULL, thread_read, &data[i]);
}
// wait for all threads to complete
for (int i = 0; i < numThreads; i++)
{
pthread_join(threads[i], NULL);
}
double endT = now();
double runtime = endT - startT;
double performance = (fsize) / (1024 * 1024 * runtime);
uint fxor=0;
for (int i = 0; i < numThreads; i++)
{
fxor^=data[i].threadXor;
}
printf("\nXOR: %08x\n", fxor);
printf("BlockSize: %ld\nPerformance: %.2f MB/s \n\n",blockSize, performance);
return 0;
}