fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <mpi.h>
  4.  
  5. #define MASTER_RANK 0
  6. #define BLUR_RADIUS 1
  7.  
  8. // Function to blur a pixel
  9. void blurPixel(unsigned char *image, int width, int x, int y) {
  10. int sum = 0;
  11. int count = 0;
  12. for (int i = x - BLUR_RADIUS; i <= x + BLUR_RADIUS; i++) {
  13. for (int j = y - BLUR_RADIUS; j <= y + BLUR_RADIUS; j++) {
  14. if (i >= 0 && i < width && j >= 0 && j < width) {
  15. sum += image[i * width + j];
  16. count++;
  17. }
  18. }
  19. }
  20. image[x * width + y] = (unsigned char)(sum / count);
  21. }
  22.  
  23. // Function to blur a block of the image
  24. void blurBlock(unsigned char *block, int width, int block_size) {
  25. for (int i = 0; i < block_size; i++) {
  26. for (int j = 0; j < block_size; j++) {
  27. blurPixel(block, width, i, j);
  28. }
  29. }
  30. }
  31.  
  32. int main(int argc, char *argv[]) {
  33. int rank, size;
  34. int image_size = 512;
  35. int block_size = 64;
  36. int num_blocks = image_size / block_size;
  37.  
  38. MPI_Init(&argc, &argv);
  39. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  40. MPI_Comm_size(MPI_COMM_WORLD, &size);
  41.  
  42. // Check if number of processes is valid
  43. if (size != num_blocks * num_blocks) {
  44. if (rank == MASTER_RANK) {
  45. printf("Number of processes must equal the number of image blocks\n");
  46. }
  47. MPI_Finalize();
  48. return EXIT_FAILURE;
  49. }
  50.  
  51. // Generate image data on the master process
  52. unsigned char *image = NULL;
  53. if (rank == MASTER_RANK) {
  54. image = (unsigned char *)malloc(image_size * image_size * sizeof(unsigned char));
  55. // Initialize image with random data
  56. for (int i = 0; i < image_size * image_size; i++) {
  57. image[i] = rand() % 256;
  58. }
  59. }
  60.  
  61. // Scatter image blocks to processes
  62. unsigned char *block = (unsigned char *)malloc(block_size * block_size * sizeof(unsigned char));
  63. MPI_Scatter(image, block_size * block_size, MPI_UNSIGNED_CHAR, block, block_size * block_size, MPI_UNSIGNED_CHAR, MASTER_RANK, MPI_COMM_WORLD);
  64.  
  65. // Process the block
  66. blurBlock(block, block_size, block_size);
  67.  
  68. // Gather processed blocks back to master process
  69. MPI_Gather(block, block_size * block_size, MPI_UNSIGNED_CHAR, image, block_size * block_size, MPI_UNSIGNED_CHAR, MASTER_RANK, MPI_COMM_WORLD);
  70.  
  71. // Print the processed image
  72. if (rank == MASTER_RANK) {
  73. printf("Processed Image:\n");
  74. for (int i = 0; i < image_size; i++) {
  75. for (int j = 0; j < image_size; j++) {
  76. printf("%3d ", image[i * image_size + j]);
  77. }
  78. printf("\n");
  79. }
  80. }
  81.  
  82. // Cleanup
  83. free(image);
  84. free(block);
  85. MPI_Finalize();
  86.  
  87. return EXIT_SUCCESS;
  88. }
  89.  
Success #stdin #stdout #stderr 0.23s 40860KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected '/' in "/"
Execution halted