-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
143 lines (127 loc) · 3.89 KB
/
main.c
File metadata and controls
143 lines (127 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
/**
* read_matrix
* -----------
* Reads an NxN matrix from a CSV file into a 1D float array.
*
* Parameters:
* filename - path to the CSV file
* mat - pointer to the float array to fill
* N - size of the matrix (NxN)
*
* Side Effects:
* Exits the program using MPI_Abort if the file can't be opened.
*/
void read_matrix(const char *filename, float *mat, int N) {
FILE *f = fopen(filename, "r");
if (!f) {
fprintf(stderr, "Error: Cannot open %s\n", filename);
MPI_Abort(MPI_COMM_WORLD, 1);
}
for (int i = 0; i < N * N; i++) {
fscanf(f, "%f", &mat[i]); // Handles without commas
fgetc(f); // Skip comma or newline
}
fclose(f);
}
/**
* write_matrix
* ------------
* Writes an NxN matrix from a 1D float array to a CSV file.
*
* Parameters:
* filename - path to the output CSV file
* mat - pointer to the float array containing the matrix
* N - size of the matrix (NxN)
*/
void write_matrix(const char *filename, float *mat, int N) {
FILE *f = fopen(filename, "w");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
fprintf(f, "%f", mat[i * N + j]);
if (j < N - 1) fprintf(f, ",");
}
fprintf(f, "\n");
}
fclose(f);
}
/**
* main
* ----
* Entry point of the MPI matrix multiplication program.
* Distributes the matrix rows among processes, performs multiplication,
* and gathers the results.
*
* Usage: ./matrix_mpi <matrix_size>
*
* Parameters:
* argc - argument count
* argv - argument vector
*
* Returns:
* 0 on success, non-zero on error (e.g., invalid matrix size).
*
* Side Effects:
* Reads/writes files, prints execution time, allocates/frees memory.
*/
int main(int argc, char* argv[]) {
int rank, size, N;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
if (argc < 2) {
if (rank == 0) fprintf(stderr, "Usage: %s <matrix_size>\n", argv[0]);
MPI_Finalize();
return 1;
}
N = atoi(argv[1]);
if (N % size != 0) {
if (rank == 0) fprintf(stderr, "Matrix size must be divisible by number of processes.\n");
MPI_Finalize();
return 1;
}
int rows_per_process = N / size;
float *A = NULL, *B = NULL, *C = NULL;
float *local_A = malloc(rows_per_process * N * sizeof(float));
float *local_C = malloc(rows_per_process * N * sizeof(float));
B = malloc(N * N * sizeof(float));
if (!B || !local_A || !local_C) {
fprintf(stderr, "Memory allocation failed\n");
MPI_Abort(MPI_COMM_WORLD, 1);
}
if (rank == 0) {
A = malloc(N * N * sizeof(float));
C = malloc(N * N * sizeof(float));
if (!A || !C) {
fprintf(stderr, "Memory allocation failed\n");
MPI_Abort(MPI_COMM_WORLD, 1);
}
read_matrix("data/matrix_a.csv", A, N);
read_matrix("data/matrix_b.csv", B, N);
}
double start = MPI_Wtime();
MPI_Bcast(B, N * N, MPI_FLOAT, 0, MPI_COMM_WORLD);
MPI_Scatter(A, rows_per_process * N, MPI_FLOAT,
local_A, rows_per_process * N, MPI_FLOAT, 0, MPI_COMM_WORLD);
for (int i = 0; i < rows_per_process; i++) {
for (int j = 0; j < N; j++) {
local_C[i * N + j] = 0.0;
for (int k = 0; k < N; k++) {
local_C[i * N + j] += local_A[i * N + k] * B[k * N + j];
}
}
}
MPI_Gather(local_C, rows_per_process * N, MPI_FLOAT,
C, rows_per_process * N, MPI_FLOAT, 0, MPI_COMM_WORLD);
if (rank == 0) {
write_matrix("data/matrix_result.csv", C, N);
double end = MPI_Wtime();
printf("Execution Time: %f seconds\n", end - start);
free(A); free(C);
}
free(B); free(local_A); free(local_C);
MPI_Finalize();
return 0;
}