Skip to content

Commit 717a8e7

Browse files
committed
Refactor
1 parent 1437119 commit 717a8e7

File tree

6 files changed

+30
-162
lines changed

6 files changed

+30
-162
lines changed
Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,14 @@
11
#include <stdio.h>
22
#include <stdlib.h>
3-
//add code here
4-
5-
//
63
#include <time.h>
74

8-
int main ()
9-
{
10-
time_t start;
11-
time_t end;
5+
// Step 1: Implement a non-parallel version of this program
6+
// Step 2: Measure the time it takes to run on large arrays
7+
// Step 3: Implement a parallel version of this program
8+
// Step 4: Measure the time again and observe the difference
129

13-
int Ncirc = 0;
14-
int num_trials = 100000000;
15-
double pi, x, y;
16-
double r = 1.0;
10+
int main() {
1711

18-
start = time(NULL);
19-
//add code here
20-
21-
//
22-
for(int i=0; i<num_trials; i++)
23-
{
24-
x = ((double) rand() / (RAND_MAX));
25-
y = ((double) rand() / (RAND_MAX));
26-
if ((x*x + y*y) <= r*r)
27-
{
28-
Ncirc++;
29-
}
30-
}
31-
end = time(NULL);
12+
// Implement a program where it calculates pi using the Monte Carlo method
3213

33-
printf("took %ld seconds to process\n", end - start);
34-
pi = 4.0 * ((double)Ncirc/(double)num_trials);
35-
printf("\n %d trials, pi is %f \n",num_trials, pi);
36-
}
14+
}
Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,15 @@
11
#include <stdio.h>
2-
//add code here
3-
4-
//
52
#include <unistd.h>
63
#include <time.h>
74

8-
int main() {
5+
// Step 1: Implement a non-parallel version of this program
6+
// Step 2: Measure the time it takes to run on large arrays
7+
// Step 3: Implement a parallel version of this program
8+
// Step 4: Measure the time again and observe the difference
99

10-
int array[20];
11-
time_t start;
12-
time_t end;
10+
int main() {
1311

14-
start = time(NULL);
15-
// add code here
16-
17-
//
18-
for(int i=0; i<sizeof(array)/sizeof(int); ++i)
19-
{
20-
array[i] = i;
21-
printf("%d\n", array[i]);
22-
sleep(1);
23-
}
24-
end = time(NULL);
12+
// Implement a program where it creates an array of 1000000 (or any large number) of elements
2513

26-
printf("took %ld seconds to process\n", end - start);
2714
return 0;
2815
}

challenges/parallel-computing/jacobi.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,27 @@ int main(int argc, char** argv)
3535
const int n = 2000;
3636
const int m = 2000;
3737
const int iter_max = 500;
38-
38+
3939
const double tol = 1.0e-6;
4040
double error = 1.0;
4141

4242
double *restrict A = (double*)malloc(sizeof(double)*n*m);
4343
double *restrict Anew = (double*)malloc(sizeof(double)*n*m);
44-
44+
4545
initialize(A, Anew, m, n);
4646

4747
printf("Jacobi relaxation Calculation: %d x %d mesh\n", n, m);
48-
48+
4949
double start = omp_get_wtime();
5050
int iter = 0;
51-
51+
5252
while ( error > tol && iter < iter_max )
5353
{
5454
error = calcNext(A, Anew, m, n);
5555
swap(A, Anew, m, n);
5656

5757
if(iter % 100 == 0) printf("%5d, %0.6f\n", iter, error);
58-
58+
5959
iter++;
6060

6161
}

challenges/parallel-computing/laplace2d.c

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,3 @@
1-
/* Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
2-
*
3-
* Redistribution and use in source and binary forms, with or without
4-
* modification, are permitted provided that the following conditions
5-
* are met:
6-
* * Redistributions of source code must retain the above copyright
7-
* notice, this list of conditions and the following disclaimer.
8-
* * Redistributions in binary form must reproduce the above copyright
9-
* notice, this list of conditions and the following disclaimer in the
10-
* documentation and/or other materials provided with the distribution.
11-
* * Neither the name of NVIDIA CORPORATION nor the names of its
12-
* contributors may be used to endorse or promote products derived
13-
* from this software without specific prior written permission.
14-
*
15-
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AS IS'' AND ANY
16-
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17-
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18-
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
19-
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20-
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21-
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22-
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23-
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24-
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25-
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26-
*/
27-
281
#include <math.h>
292
#include <stdlib.h>
303

@@ -35,7 +8,7 @@ void initialize(double *restrict A, double *restrict Anew, int m, int n)
358
memset(A, 0, n * m * sizeof(double));
369
memset(Anew, 0, n * m * sizeof(double));
3710

38-
11+
// Can you parallelize this loop?
3912
for(int i = 0; i < m; i++){
4013
A[i] = 100.0;
4114
Anew[i] = 100.0;
@@ -45,29 +18,15 @@ void initialize(double *restrict A, double *restrict Anew, int m, int n)
4518
double calcNext(double *restrict A, double *restrict Anew, int m, int n)
4619
{
4720
double error = 0.0;
48-
49-
for( int j = 1; j < n-1; j++)
50-
{
51-
for( int i = 1; i < m-1; i++ )
52-
{
53-
Anew[OFFSET(j, i, m)] = 0.25 * ( A[OFFSET(j, i+1, m)] + A[OFFSET(j, i-1, m)]
54-
+ A[OFFSET(j-1, i, m)] + A[OFFSET(j+1, i, m)]);
55-
error = fmax( error, fabs(Anew[OFFSET(j, i, m)] - A[OFFSET(j, i , m)]));
56-
}
57-
}
21+
22+
// Implementation here
23+
5824
return error;
5925
}
6026

6127
void swap(double *restrict A, double *restrict Anew, int m, int n)
6228
{
63-
64-
for( int j = 1; j < n-1; j++)
65-
{
66-
for( int i = 1; i < m-1; i++ )
67-
{
68-
A[OFFSET(j, i, m)] = Anew[OFFSET(j, i, m)];
69-
}
70-
}
29+
// Implementation here
7130
}
7231

7332
void deallocate(double *restrict A, double *restrict Anew)
Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,15 @@
11
#include <stdio.h>
2-
//add code here
3-
4-
5-
//
62
#include <unistd.h>
73
#include <time.h>
84

9-
int main() {
5+
// Step 1: Implement a non-parallel version of this program
6+
// Step 2: Measure the time it takes to run on large arrays
7+
// Step 3: Implement a parallel version of this program
8+
// Step 4: Measure the time again and observe the difference
109

11-
int array[20];
12-
time_t start;
13-
time_t end;
14-
int x;
15-
int sum = 0;
16-
17-
start = time(NULL);
18-
//add code here
19-
20-
//
21-
for(int i=0; i<sizeof(array)/sizeof(int); ++i)
22-
{
23-
array[i] = i;
24-
sleep(1);
10+
int main() {
2511

26-
//add code here
27-
x = pow(array[i],2);
28-
29-
//
30-
}
31-
printf("sum of the array is %d\n", sum);
32-
if (sum == 2470)
33-
{
34-
printf("correct\n");
35-
}
36-
else
37-
{
38-
printf("wrong answer\n");
39-
}
40-
41-
end = time(NULL);
12+
// Implement a program where it squares each element in the array and outputs the sum of them
4213

43-
printf("took %ld seconds to process\n", end - start);
4414
return 0;
4515
}

challenges/parallel-computing/reduction.c

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,6 @@
77

88
int main() {
99

10-
int array[20];
11-
time_t start;
12-
time_t end;
10+
// Implement a program where it sums all the elements in the array
1311

14-
//add code here
15-
16-
//
17-
start = time(NULL);
18-
19-
//add code here
20-
21-
//
22-
for(int i=0; i<sizeof(array)/sizeof(int); ++i)
23-
{
24-
array[i] = i;
25-
sleep(1);
26-
27-
//add code here
28-
29-
//
30-
}
31-
32-
printf("sum of the array is %d\n", sum);
33-
34-
end = time(NULL);
35-
36-
printf("took %ld seconds to process\n", end - start);
37-
return 0;
3812
}

0 commit comments

Comments
 (0)