forked from r3kall/transactional-memory-apriori
-
Notifications
You must be signed in to change notification settings - Fork 0
/
offsets.c
82 lines (70 loc) · 2.22 KB
/
offsets.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
int main(int argc, char* argv[]) {
int i, j, err, nprocs, num_trans, maxitem, avg_trans_sz, numitem;
int *count, *buf, *buf_ptr;
long fsize;
FILE *fptr;
FILE *optr;
if (argc != 4) {
printf("Usage: %s filename nprocs path\n",argv[0]);
exit(1);
}
nprocs = atoi(argv[2]);
fptr = fopen(argv[1], "r");
if (fptr < 0){
printf("Error: open file %s (%d)\n", argv[1],strerror(errno));
exit(-1);
}
/* input file format is in binary
* First 4-byte integer is the number of total transactions
* Second 4-byte integer is the number of unique items
* Third 4-byte integer is the average size of transactions
* The rest are transactions (one transaction appended after another)
* Each transaction contains
* a transaction ID (a 4-byte integer),
* the number of items (a 4-byte integer),
* the list of item IDs (each is a 4-byte integer)
*/
fread(&num_trans, sizeof(int), 1, fptr);
fread(&maxitem, sizeof(int), 1, fptr);
fread(&avg_trans_sz, sizeof(int), 1, fptr);
fseek(fptr, 0, SEEK_END);
fsize = ftell(fptr);
fsize -= 3 * sizeof(int); /* header of 3 int */
buf = (int*) malloc(fsize);
fseek(fptr, 3 * sizeof(int), SEEK_SET);
fread(buf, 1, fsize, fptr);
fclose(fptr);
count = (int*) calloc(nprocs, sizeof(int));
for (i=0; i<nprocs; i++) {
count[i] = num_trans / nprocs;
if (i < num_trans % nprocs)
count[i]++;
}
char* outfile = (char*) malloc(256);
strcpy(outfile, argv[3]);
strcat(outfile, "/offset_P");
strcat(outfile, argv[2]);
strcat(outfile, ".txt");
if ((optr = fopen (outfile, "w")) == NULL){
printf("can't open %s\n", outfile);
exit(-1);
}
fprintf(optr, "0\n");
buf_ptr = buf;
for (i=0; i<nprocs; i++) {
for (j=0; j<count[i]; j++) {
buf_ptr++; /* transaction ID */
numitem = *buf_ptr; /* number of items */
buf_ptr += numitem + 1;
}
fprintf(optr, "%d\n", buf_ptr-buf);
}
free (buf);
free (count);
fclose(optr);
return 0;
}