The document provides a code sample to count the frequency of a target number in a dataset using MPI for parallelization. The master process reads the input data, divides it evenly among the slave processes, and each slave counts the frequency in its portion. The slaves send their local counts to the master which sums them and prints the total count.
Capitol Tech U Doctoral Presentation - April 2024.pptx
I have come code already but I cant quite get the output rig.pdf
1. I have come code already but I cant quite get the output right if it helps here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
// frequency function
int freq(int *data, int size, int number)
{
// declare count variable
int count;
// iterate through data file
int i =0;
for (; i < size; i++)
{
// if data target number count it
if (data[i] == number)
{
count++;
}
}
return count;
}
int main(int argc, char **argv)
{
// declare data variables
int *data = NULL;
int *localData = NULL;
int localSize = 0, localCount = 0, totalCount = 0;
int number;
// declare MPI Variables
int rank; // processor rank
int size; // size of cluster
MPI_Status status; // MPI status object
// initalize the MPI environment
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
// when process rank 0 is active read data file and distribute data
if (rank == 0) {
// get number to count from user input
number = atoi(argv[argc-1]);
// open data file and read data
FILE *fp = fopen("data.dat", "r");
2. int totalSize;
fscanf(fp, "%d", &totalSize);
data = (int *) malloc(totalSize * sizeof(int));
int i =0;
for (; i < totalSize; i++)
{
fscanf(fp, "%d", &data[i]);
}
fclose(fp);
// compute block size for each process
int blockSize = totalSize / size;
// send block size and data to each process
for (i = 1; i < size; i++)
{
int start = i * blockSize;
MPI_Send(&blockSize, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
MPI_Send(&data[start], blockSize, MPI_INT, i, 0, MPI_COMM_WORLD);
}
localSize = blockSize;
localData = data;
}
// when slave process is active receive data
else
{
MPI_Recv(&localSize, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
localData = (int *) malloc(localSize * sizeof(int));
MPI_Recv(localData, localSize, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
}
// count frequency of number in the local memory of each processor
localCount = freq(localData, localSize, number);
// gather local counts from all processes to master process
MPI_Reduce(&localCount, &totalCount, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
// print frequency count for each process
printf("My id is %d and the frequency of %d is %dn", rank, number, localCount);
// when master process is active print total frequency count
if (rank == 0)
{
printf("##################################n");
printf("The total frequency of %d is %dn", number, totalCount);
// free allocated memory
free(data);
}
27. slaves finish their counting, they should pass their results to the master and displays the count of
each slave. The master displays the total count of the number. Your MPI code should be flexible
enough to run with any number of processor(s) for any input size. You should decompose the
input data as uniform as possible. The output example with 5 processors is shown below. Submit
your report with source code. Your report should explain your code such as code structure,
parallelization method (i.e., data decomposition), data distribution, and collection (i.e.,
communication method) with your code. In the report, you should include the sample output of
your code. Your grade is based on your explanation in your report and completion of the code. 10
points for the code algorithm, logic, and explanation 10 points for the completion of job Example
output: It finds the total count of number 11 in the file using 4 slave processors. Each slave
processors display its count of 11. Master displays "number to find" and "total number of 11 " in
the file. Number to find is 11 My id is 1 and the frequency of 11 is 1 My id is 2 and the frequency of
11 is 3 My id is 3 and the frequency of 11 is 7 My id is 4 and the frequency of 11 is 2 The total
frequency of 11 is 13