Subash Kumar Rawat - 230155 - WPS02

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

University of Wolverhampton

School of Mathematics and Computer Science

5CS022 Distribute and Cloud Systems Programming Week 2 Workshop

Overview

The aim of this workshop is to build on the previous workshop with MPI and to carry out an assessed task.
You can carry out this workshop either the university servers: thinlinc.wlv.ac.uk or your own Linux system
(if you prefer to use Putty to log in instead of the Thinlinc client, connect to the server tl-01.wlv.ac.uk
instead). Tasks

1. The following C program sums up all the values in array "data" and displays the sum total.:

#include <stdio.h>

#define NUMDATA 10000 int


data[NUMDATA];

void LoadData(int data[])


{ for(int i = 0; i < NUMDATA; i++){ data[i]
= 1;
}
}

int AddUp(int data[], int count)


{ int sum = 0; for(int i = 0; i <
count; i++){ sum += data[i];
}
return sum;
}

int main(void) { int


sum;

LoadData(data); sum = AddUp(data, NUMDATA);


printf("The total sum of data is %d\n",
sum);
return 0;
}

Convert it to MPI to run with any number of nodes including just one.

5CS022 Distributed and Cloud Systems Programming 1


5CS022 Distributed and Cloud Systems Programming 2
Ans

2. Write an MPI program called pingpong.c to run with exactly 2 processes. Process rank 0 is to send an integer
variable called "ball" initialised with the value zero to Process rank 1. Process rank 1 will add 1 to the ball and
send it back. This will repeat until the ball has a value of 10 in Process rank 0.

5CS022 Distributed and Cloud Systems Programming 3


Output

5CS022 Distributed and Cloud Systems Programming 4


3. Write a "Pass-the-parcel" MPI program that will run with 3 or more nodes, such that Process rank 0 will send an
integer variable call "parcel" initialised with 1, to Process rank 1 which will add 1 to the parcel and then send it to
Process rank 2, and so on until the highest rank process will send it back to Process rank 0, at which point the
parcel variable should contain the value of the number of nodes there are.

Ans

5CS022 Distributed and Cloud Systems Programming 5


Output

5CS022 Distributed and Cloud Systems Programming 6


4. The following program has all the processes with rank greater than 0 send random amounts of data to Process
rank 0 :

#include <mpi.h>

#include <stdio.h>
#include <stdlib.h> #define

NUMDATA 1000

int main(int argc, char **argv)


{ int size; int
rank; int
tag=0; int
count;
MPI_Status status;
int data[NUMDATA];

MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);

if (rank == 0){ for(int i = 0; i <


size - 1; i++) {
MPI_Recv(data, NUMDATA, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG,
MPI_COMM_WORLD, &status); MPI_Get_count(&status, MPI_INT,
&count);
printf("Node ID: %d; tag: %d; MPI_Get_count: %d; \n",
status.MPI_SOURCE, status.MPI_TAG, count);
}
}
else
{
MPI_Send(data, rand()%100, MPI_INT, 0, tag, MPI_COMM_WORLD);
}
MPI_Finalize();
return 0;
}

Modify it so that it doesn't use a fixed size data buffer but uses "malloc()" to allocate the correct amount
of memory to use every time. Note this means that Process rank 0 will need to know what the amount of
data it is expecting before it receives it. Ans:

5CS022 Distributed and Cloud Systems Programming 7


5CS022 Distributed and Cloud Systems Programming 8
Assessed Workshop Task
5. The file "WarAndPeace.txt" on Canvas contains the entire text of the book "War and Peace" by Leo Tolstoy. Write
an MPI program to count the number of times each letter of the alphabet occurs in the book. Count both the upper
case and the lowercase as the same. Ignore any letter with accents such as " é " and so on.

Your MPI program should work with any number of processes from 1 to 100 processes. Only Process rank 0
(zero) should read in the file and send the appropriate chunk of file to each other process. The other processes
should not read in the file.

You should submit this program as "workshoptask1.c" as part of your final portfolio submission. You can also
upload it to the formative submission point for formative feedback.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "mpi.h"

#define ALPHABET_SIZE 26

int main(int argc, char *argv[]) {


int process_rank, process_size;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &process_rank);
MPI_Comm_size(MPI_COMM_WORLD, &process_size);
if (process_size < 1 || process_size >
100) { if (process_rank == 0)
printf("Invalid number of processes. Please run with 1 to 100
processes.\n"); MPI_Finalize(); return 1;

5CS022 Distributed and Cloud Systems Programming 9


}

FILE *file; if (process_rank == 0) {


file = fopen("WarAndPeace.txt", "r");
if (file == NULL) {
printf("Failed to open
file.\n"); MPI_Finalize();
return 1;
}
} long file_size;
if (process_rank == 0) {
fseek(file, 0, SEEK_END);
file_size = ftell(file);
fseek(file, 0, SEEK_SET);
}
MPI_Bcast(&file_size, 1, MPI_LONG, 0, MPI_COMM_WORLD);
int portionsize = (file_size + process_size - 1) / process_size; // ceil(file_size
/ size) char *portionsizes_ptr = (char*)malloc(portionsize * sizeof(char)); if
(portionsizes_ptr == NULL) {
printf("Memory allocation
failed.\n"); MPI_Finalize();
return 1;
}

MPI_File mpi_file;
MPI_File_open(MPI_COMM_WORLD, "WarAndPeace.txt", MPI_MODE_RDONLY, MPI_INFO_NULL,
&mpi_file);
MPI_File_set_view(mpi_file, process_rank * portionsize, MPI_CHAR, MPI_CHAR, "native",
MPI_INFO_NULL);
MPI_File_read_all(mpi_file, portionsizes_ptr, portionsize, MPI_CHAR,
MPI_STATUS_IGNORE);
MPI_File_close(&mpi_file);

int *counts_ptr = (int*)calloc(ALPHABET_SIZE,


sizeof(int)); for (int i = 0; i < portionsize; i++) {
char c = tolower(portionsizes_ptr[i]); if (isalpha(c))
{
counts_ptr[c - 'a']++;
}
}

// Reduce counts to process_rank 0


int *global_counts_ptr = (int*)calloc(ALPHABET_SIZE, sizeof(int));
MPI_Reduce(counts_ptr, global_counts_ptr, ALPHABET_SIZE, MPI_INT, MPI_SUM, 0,
MPI_COMM_WORLD);

5CS022 Distributed and Cloud Systems Programming 10


// Print counts in process_rank 0 if (process_rank ==
0) { printf("Letter Counts:\n"); for (int i =
0; i < ALPHABET_SIZE; i++) { printf("%c: %d\n",
'a' + i, global_counts_ptr[i]);
}
}

// Cleanup
free(portionsizes_ptr);
free(counts_ptr); if (process_rank
== 0) fclose(file);
MPI_Finalize();
return 0;
}

5CS022 Distributed and Cloud Systems Programming 11


5CS022 Distributed and Cloud Systems Programming 12

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy