M_faisal_lab_09.docx

  • Uploaded by: Muhammad Ali
  • 0
  • 0
  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View M_faisal_lab_09.docx as PDF for free.

More details

  • Words: 562
  • Pages: 2
CSL-342: Parallel Programming lab Semester BSIT – 06 Student Name: Muhammad Faisal Registration: 43865

Lab 09: Performing Parallel Rank with MPI

Exercises Exercise 1 Write a program to finding the standard deviation of a distributed set of numbers. For those that may have forgotten, standard deviation is a measure of the dispersion of numbers from their mean. A lower standard deviation means that the numbers are closer together and vice versa for higher standard deviations. Code #include <stdio.h> #include <stdlib.h> #include <mpi.h> #include <math.h> #include // Creates an array of random numbers. Each number has a value from 0 - 1 float *create_rand_nums(int num_elements) { float *rand_nums = (float *)malloc(sizeof(float) * num_elements); assert(rand_nums != NULL); int i; for (i = 0; i < num_elements; i++) { rand_nums[i] = (rand() / (float)RAND_MAX); } return rand_nums; } int main(int argc, char** argv) { if (argc != 2) { fprintf(stderr, "Usage: avg num_elements_per_proc\n"); exit(1); } int num_elements_per_proc = atoi(argv[1]); MPI_Init(NULL, NULL); int world_rank; MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); int world_size; MPI_Comm_size(MPI_COMM_WORLD, &world_size); // Create a random array of elements on all processes. srand(time(NULL)*world_rank); // Seed the random number generator of processes uniquely float *rand_nums = NULL; rand_nums = create_rand_nums(num_elements_per_proc); // Sum the numbers locally float local_sum = 0; int i; for (i = 0; i <

#include <stdio.h> #include <stdlib.h> #include <mpi.h> #include #include // Creates an array of random numbers. Each number has a value from 0 - 1 float *create_rand_nums(int num_elements) { float *rand_nums = (float *)malloc(sizeof(float) * num_elements); assert(rand_nums != NULL); int i; for (i = 0; i < num_elements; i++) { rand_nums[i] = (rand() / (float)RAND_MAX); } return rand_nums; } int main(int argc, char** argv) { if (argc != 2) { fprintf(stderr, "Usage: avg num_elements_per_proc\n"); exit(1); } int num_elements_per_proc = atoi(argv[1]); MPI_Init(NULL, NULL); int world_rank; MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); int world_size; MPI_Comm_size(MPI_COMM_WORLD, &world_size); // Create a random array of elements on all processes. srand(time(NULL)*world_rank); // Seed the random number generator to get different results each time for each processor float *rand_nums = NULL; rand_nums = create_rand_nums(num_elements_per_proc);

num_elements_per_proc; i++) { local_sum += rand_nums[i]; } // Reduce all of the local sums into the global sum in order to // calculate the mean float global_sum; MPI_Allreduce(&local_sum, &global_sum, 1, MPI_FLOAT, MPI_SUM, MPI_COMM_WORLD); float mean = global_sum / (num_elements_per_proc * world_size); // Compute the local sum of the squared differences from the mean float local_sq_diff = 0; for (i = 0; i < num_elements_per_proc; i++) { local_sq_diff += (rand_nums[i] - mean) * (rand_nums[i] - mean); } // Reduce the global sum of the squared differences to the root process // and print off the answer float global_sq_diff; MPI_Reduce(&local_sq_diff, &global_sq_diff, 1, MPI_FLOAT, MPI_SUM, 0, MPI_COMM_WORLD); if (world_rank == 0) { float stddev = sqrt(global_sq_diff / (num_elements_per_proc * world_size)); printf("Mean - %f, Standard deviation = %f\n", mean, stddev); } // Clean up free(rand_nums); MPI_Barrier(MPI_COMM_WORLD); MPI_Finalize(); }

Output:

float local_sum = 0; int i; for (i = 0; i < num_elements_per_proc; i++) { local_sum += rand_nums[i]; } // Print the random numbers on each process printf("Local sum for process %d - %f, avg = %f\n", world_rank, local_sum, local_sum / num_elements_per_proc); // Reduce all of the local sums into the global sum float global_sum; MPI_Reduce(&local_sum, &global_sum, 1, MPI_FLOAT, MPI_SUM, 0, MPI_COMM_WORLD); // Print the result if (world_rank == 0) { printf("Total sum = %f, avg = %f\n", global_sum, global_sum / (world_size * num_elements_per_proc)); } // Clean up free(rand_nums); MPI_Barrier(MPI_COMM_WORLD); MPI_Finalize(); }

More Documents from "Muhammad Ali"