Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <mpi.h>
- #define WIDTH 5
- #define HEIGHT 6
- #define DEPTH 7
- #define ITERS 1
- int main(int argc, char** argv) {
- MPI_Init(&argc, &argv);
- int proc_number = 0; //номер процесса
- int proc_count = 0; //кол. проц.
- MPI_Comm_rank(MPI_COMM_WORLD, &proc_number);
- MPI_Comm_size(MPI_COMM_WORLD, &proc_count);
- if (proc_count > WIDTH) {
- printf("Too many proccess for WIDTH of %d", WIDTH);
- MPI_Finalize();
- return 0;
- }
- int width = 0;
- int height = HEIGHT;
- int depth = DEPTH;
- width = WIDTH / proc_count;
- if (proc_number < WIDTH % proc_count) {
- width += 1;
- }
- width += 2;
- height += 2;
- depth +=2;
- //---
- double arr[width][height][depth];
- double arrNew[width][height][depth];
- int i, j, t;
- for (i = 0; i < width; i++) {
- for (j = 0; j < height; j++) {
- for (t = 0; t < depth; t++) {
- if ((proc_number == 0 && i == 0) || (j == 0) || (t == 0) ||
- (proc_number == proc_count - 1 && i == width - 1) || (j == height - 1) || (t == depth - 1)) {
- arr[i][j][t] = 1;
- arrNew[i][j][t] = 1;
- } else {
- arr[i][j][t] = 0;
- arrNew[i][j][t] = 0;
- }
- }
- }
- //---
- int iter;
- for (iter = 0; iter < ITERS; iter++) {
- for (i = 1; i < width - 1; i++) {
- for (j = 1; j < height - 1; j++) {
- for (t = 1; t < depth - 1; t++) {
- arrNew[i][j][t] = arr[i][j - 1][t] + arr[i][j + 1][t];
- arrNew[i][j][t] += arr[i - 1][j][t] + arr[i + 1][j][t];
- arrNew[i][j][t] += arr[i][j][t - 1] + arr[i][j][t + 1];
- arrNew[i][j][t] /= 6.0;
- }
- }
- }
- //---
- int tt;
- int displs[proc_count];
- if (proc_number > 0) {
- int counts[proc_count];
- for (tt = 0; tt < proc_count; tt++) {
- counts[tt] = 0;
- }
- counts[proc_number - 1] = depth - 2;
- for (tt = 0; tt < proc_count; tt++) {
- displs[tt] = 0;
- }
- for (i = 1; i < height - 1; i++){
- MPI_Alltoallv(&arrNew[1][i][1], counts, displs, MPI_DOUBLE,
- &arr[0][i][1], counts, displs, MPI_DOUBLE, MPI_COMM_WORLD);
- }
- }
- if (proc_number < proc_count - 1) {
- int counts[proc_count];
- for (tt = 0; tt < proc_count; tt++) {
- counts[tt] = 0;
- }
- counts[proc_number + 1] = depth - 2;
- int displs[proc_count];
- for (tt = 0; tt < proc_count; tt++) {
- displs[tt] = 0;
- }
- for (i = 1; i < height -1; i++) {
- MPI_Alltoallv(&arrNew[width - 2][i][1], counts, displs, MPI_DOUBLE,
- &arr[width - 1][i][1], counts, displs, MPI_DOUBLE, MPI_COMM_WORLD);
- }
- }
- for (i = 1 ; i < width - 1; i++)
- for (j = 1 ; j < height - 1; j++)
- for (t = 1; t < depth - 1; t++)
- arr[i][j][t]=arrNew[i][j][t];
- }
- //---
- if (proc_number == 0) {
- fclose(fopen("output.txt", "w"));
- }
- int k;
- for (k = 0; k < proc_count; k++) {
- MPI_Barrier(MPI_COMM_WORLD);
- if (k == proc_number) {
- FILE* out = fopen("output.txt", "a");
- int start = 1;
- if (proc_number == 0) {
- start = 0;
- }
- int end = width - 1;
- if (proc_number == proc_count - 1) {
- end = width;
- }
- for (i = start; i < end; i++) {
- for (j = 0; j < height; j++) {
- for (t = 0; t < depth; t++) {
- fprintf(out, "%lf ", arr[i][j][t]);
- }
- fprintf(out, "\n");
- }
- }
- //fprintf(out, "\n---\n\n");
- fclose(out);
- }
- }
- MPI_Finalize();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement