fork download
  1. // Libraries
  2. #include <iostream> // Library for input and output operations.
  3. #include <omp.h> // Library OpenMP for parallel programming.
  4. #include <cstdlib> // Library for general utilities like rand() and srand().
  5. #include <ctime> // Library for time() to seed the random number generator.
  6.  
  7. // Constants
  8. #define ARRAY_SIZE 1000 // Size of the arrays.
  9. #define CHUNK_SIZE 100 // Size of chunks for parallel processing.
  10. #define NUM_DISPLAY 10 // Number of elements to display.
  11.  
  12. // Namespace
  13. using namespace std; // To not specify explicitly the std namespace for every standard library object or function.
  14.  
  15.  
  16. // Function to print the specified first elements of an array.
  17. void print_array(float *array)
  18. {
  19. // Loop through the specified first elements.
  20. for(int i=0; i<NUM_DISPLAY; i++)
  21. {
  22. // Print the current element.
  23. cout<<array[i];
  24.  
  25. // Print the separator if it's not the last element to print.
  26. if(i<NUM_DISPLAY-1)
  27. {
  28. cout<<" , ";
  29. }
  30. }
  31. // Print a newline at the end.
  32. cout<<endl;
  33. }
  34.  
  35.  
  36. // Main
  37. int main()
  38. {
  39. // Display message to inform that it is a parallel operation.
  40. cout<<"ADDITION OF ARRAYS IN PARALLEL\n";
  41.  
  42. // Declare arrays.
  43. float array_A[ARRAY_SIZE], array_B[ARRAY_SIZE], array_R[ARRAY_SIZE];
  44.  
  45. // Seed the random number generator.
  46. srand(static_cast<unsigned>(time(0)));
  47.  
  48. // Fill the arrays with random float values between 0 and 100.
  49. for(int i=0; i<ARRAY_SIZE; i++)
  50. {
  51. array_A[i] = static_cast<float>(rand()) / RAND_MAX * 100;
  52. array_B[i] = static_cast<float>(rand()) / RAND_MAX * 100;
  53. }
  54.  
  55. // Set the chunk size for parallel processing.
  56. int chunk_size = CHUNK_SIZE;
  57.  
  58. // Parallel region to add the elements of arrays A and B into resultArray
  59. #pragma omp parallel for \
  60.   shared(arrayA, arrayB, resultArray, chunkSize) private(index) \
  61.   schedule(static, chunkSize)
  62.  
  63. // Add corresponding elements of array_A and array_B.
  64. for(int i=0; i<ARRAY_SIZE; i++)
  65. {
  66. array_R[i] = array_A[i] + array_B[i];
  67. }
  68.  
  69. // Print the specified first elements of each array.
  70. cout<<"\nFirst "<<NUM_DISPLAY<<" elements of Array A: "<<endl;
  71. print_array(array_A);
  72. cout<<"\nFirst "<<NUM_DISPLAY<<" elements of Array B: "<<endl;
  73. print_array(array_B);
  74. cout<<"\nFirst "<<NUM_DISPLAY<<" elements of the Resultant Array: "<<endl;
  75. print_array(array_R);
  76.  
  77. // Exit the program.
  78. return 0;
  79. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
ADDITION OF ARRAYS IN PARALLEL

First 10 elements of Array A: 
49.3816 , 52.8531 , 41.1642 , 67.2859 , 47.5522 , 29.1132 , 35.7722 , 99.2551 , 40.9164 , 30.9634

First 10 elements of Array B: 
89.1679 , 78.3806 , 77.2719 , 21.6129 , 70.5699 , 54.7713 , 86.8713 , 65.1435 , 40.0056 , 64.0346

First 10 elements of the Resultant Array: 
138.549 , 131.234 , 118.436 , 88.8989 , 118.122 , 83.8845 , 122.643 , 164.399 , 80.922 , 94.998