-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsum.cpp
More file actions
105 lines (89 loc) · 2.48 KB
/
sum.cpp
File metadata and controls
105 lines (89 loc) · 2.48 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
/*
A multi-threaded solution for computing the sum of a large array of integers.
Takes two arguments from command line:
1. Name of file containing the integers (one number per line)
2. <T> is number of threads to be created
Built under assumptions that:
input contains N integers where N <= 1 000 000
T <= N
*/
#include <iostream>
#include <fstream>
using namespace std;
int totalSum=0;
int numArr[1000000];
struct sum_struct
{
int first;
int last;
int sum;
};
void* adder(void* arg)
{
//cast arg from void* to desired type
//cout<<"Entering adder function\n";
struct sum_struct *arg_struct = (struct sum_struct*) arg;
arg_struct->sum =0;
//cout<<"Starting integer: "<<arg_struct->smallArr[0];
//cout<<"First element: "<<arg_struct->first<<" Last element: "<<arg_struct->last;
for(int i = arg_struct->first; i <= (arg_struct->last); i++)
{
//cout<<"Entered loop with i: "<<i<<endl;
arg_struct-> sum += numArr[i];
}
totalSum+=arg_struct->sum;
cout<<arg_struct->sum<<endl;
pthread_exit(0);
}
int main(int argc, char * argv[])
{
if(argc !=3){
printf("Wrong amount of arguments, please try again");
}
string filename;
filename = argv[1];
unsigned long numThreads = atol(argv[2]);
pthread_t tid[numThreads];
//create multithreads
sum_struct args[numThreads];
string line;
int arrSize = 0;
//open file for reading
ifstream file;
file.open(filename);
if(file.is_open()){
//cout<<"here";
while(true){
int num;
file>>num;
if(file.eof()) break;
numArr[arrSize++]=num;
}
//for(int i = 0; i < arrSize; i++) cout <<numArr[i]<<" ";
//cout<<endl;
file.close();
}
else cout<<"unable to open file";
//done creating array of integers from file
for(int i=0;i<numThreads;i++){
if(i==0){ //our first amount
args[i].first = i;
args[i].last = args[i].first + arrSize/numThreads;
}else if(i<(arrSize%numThreads)){//while N%T containing N/T + 1 number of elements
args[i].first = args[i-1].last+1;
args[i].last = args[i].first + arrSize/numThreads;
}else{ //remaining T-N%T groups will contain N/T elements
args[i].first = args[i-1].last+1;
args[i].last = args[i].first + arrSize/numThreads - 1;
if(args[i].last>=arrSize){
args[i].last = arrSize - 1;
}
}
cout<<"Thread "<<i+1<<": ";
pthread_create(&tid[i], NULL, adder, &args[i]);
// kill all the threads after we are done using them
pthread_join(tid[i],NULL);
}
cout<<"Sum = "<<totalSum<<endl;
return 0;
}