-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhw3.txt
More file actions
185 lines (154 loc) · 3.47 KB
/
hw3.txt
File metadata and controls
185 lines (154 loc) · 3.47 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
#include "stdio.h"
#include "sys/types.h"
#include "sys/times.h"
#include "ulocks.h"
#include "sys/param.h"
#include <time.h>
#define TYP double
/*
*This program will produce the prefix sum of an array.
* Written for IRIX 6.5, SGI
Given an array A[0..n-1],
i. partition into p subarrays
ii. local sums of individual subarrays
iii. find prefix sums on local sums(ii)
iv. use last index of left neighbor to calc prefix sum
of own subarrays
*
*/
TYP summation(TYP *p, TYP *r);
void prefix_sum(TYP *p, TYP *r, TYP sum);
void populate_matrices(TYP *ptr, int N);
void slaveproc(TYP *sub_pfx_arr, int N, TYP *A);
void print(TYP *arr, int N);
int npcs;
/*
*timing elements
*/
struct tms tim;
float ptime=0, stime=0, speedup;
int main(int argc, char **argv)
{
int N = atoi(argv[1]);
TYP *A = (TYP*) malloc(N * sizeof *A);
populate_matrices(A, N);
// print(A, N);
/*
*getting the requested number of
*processors from the terminal
*/
npcs=atoi(argv[2]);
if(npcs>N)
npcs = N;
/*
*requesting the cpus from the machine
*/
m_set_procs(npcs);
/*
*setting numprocs to be the actual
*number of cpus the machine has
*provided. Can be different from
*the requested amount.
*/
npcs=m_get_numprocs();
TYP *sub_pfx_arr = (TYP*)malloc(npcs * sizeof *sub_pfx_arr);
/*
*sequential prefix sum
*/
stime=times(&tim);
int i;
for(i=0; i<10000; i++)
prefix_sum(A, &A[N], 0);
stime=times(&tim)-stime;
// print(A, N);
populate_matrices(A, N);
/*
*prefix sums in parallel
*/
ptime=times(&tim);
m_fork(slaveproc, sub_pfx_arr,N, A);
m_kill_procs();
ptime=times(&tim)-ptime;
// print(A, N);
free(sub_pfx_arr);
printf("Numprocs: %d N: %d Sequential time: %.1f Parallel time: %.1f Spee
dup: %f\n"
,npcs,N, stime, ptime, stime/ptime);
return 1;
}
void slaveproc(TYP *sub_pfx_arr, int N, TYP *A)
{
int stride, subidx;
TYP sum, *p, lsum;
int id;
stride = N/npcs;
id= m_get_myid();
subidx = N/npcs*id;
/*
*last processor gets leftover elements
*/
if(id==npcs-1) stride+=N%npcs;
int i;
for(i=0; i<10000; i++)
{
*(sub_pfx_arr+id)=summation(A+subidx, A+subidx+stride);
/*SYNCHRONIZATION HERE*/
m_sync();
/*
*find prefix sums on local sums
*/
p=A+subidx+stride-1;
*p=*(sub_pfx_arr+id);
if(id!=0)
{
int i;
for(i=0; i<id; i++)
*p+= *(sub_pfx_arr+i);
}
/*
*All processors except 0 will grab left neighbor sum
*/
lsum=0;
if(id!=0)
lsum=*(p-stride);
/*
*All processors calculate own prefix sums (iv)
*/
prefix_sum(A+subidx, A+subidx+stride-1, lsum);
/* for(p=A+subidx;p<A+subidx+stride-1;p++)
* {
* *p+=lsum;
* lsum=*p;
* }*/
}
}
void prefix_sum(TYP *p, TYP *r, TYP sum)
{
int i=0;
for(; p<r; p++)
{
// printf("%d i\n", i++);
*p+=sum;
sum=*p;
}
}
TYP summation(TYP *p, TYP *r)
{
TYP sum=0;
for(; p<r; p++)
sum+=*p;
return sum;
}
void populate_matrices(TYP *ptr, int N)
{
int i;
while(i<N)
*(ptr++)=i++;
}
void print(TYP *arr, int N)
{
int i;
for(i=0; i<N; i++)
printf("%d ", arr[i]);
printf("\n");
}