Programing Assignment Report-121231

Contents

Part 1. 3

Part 2. 5

Part 3. 6

Part 4. 7

Part 1

In the part 1 of the problem, the code has been modified to print the boundary conditions which has been specified in the problem. The boundary conditions are printed at the start of the program, using following code:

printf(“The boundary conditions are:\n”);

printf(“Top: %f\n”, t[0][1]);

printf(“Bottom: %f\n”, t[41][1]);

printf(“Left: %f\n”, t[1][0]);

printf(“Right: %f\n”, t[1][9]);

The array is accessed for the location which stored the respective boundary condition, and hence, they print the boundary condition.

The execution time of the code is calculated using time.h header file. The header file uses the system clock time to calculate the execution time. The variable is defined to take the input for start and end of the execution of the code, and hence, print the execution time as a difference of the two times, in seconds.

A status variable has been defined to take the input from user if they want to print the results of the solution. The default value of the variable is set to be 1, which prints out the result. It can be changed by the user, to print or not to print the result. An if-else statement has been used for the same:

// print results

if (printStatus == 1){

printf(“iter = %d  difmax = %9.11lf”, iter, difmax);

for (i=0; i <= m+1; i++) {

printf(“\n”);

for (j=0; j <= n+1; j++) {

printf(“%3.5lf “, t[i][j]);

}

}

}

else{

printf(“Results not printed.”);

}

The output of the program when results are printed

Part 2

In this part, the OMP header file is included in the program. The input for number of thread is taken from the user using following code:

printf(“Enter the number of threads (max 10): “);

scanf(“%d”,&nthreads);

The number of threads to be used is then started using following code:

omp_set_num_threads(nthreads);

This makes the program run in a parallel version.

The other features are kept same as part 1, as per the question. The output for 1, 2, 3 and 4 threads, and without printing the result is as follows:

output

Part 3

In this part, the performance test is completed on the code produced. To optimize the code, we have removed the print statements for result, the status check variable and its print and scan functions. Hence, the program is expected to speed up. The test run for 1 thread and 8 threads are done. The output is as follows:

output1

Here, we obtain a difference of 0.01 seconds.

Part 4

The code has been further optimised here. All the print statements has been removed. The maximum size has been predefined in the variable to speed up the process. The arrays are predefined as well.

Here, we can see that the difference in the output for 1 and 8 threads is just 0.008 seconds. Thus, the code has been properly optimized.

Part Threads Time taken (seconds)
3 1 0.471916
3 8 0.488292
4 1 0.478789
4 8 0.470841