Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. void runMatrixExperimentOMP(unsigned int min,
  2. unsigned int max,
  3. unsigned int threads,
  4. unsigned int repeats,
  5. std::string file_out) {
  6. std::cout << "Matrix multipication OpenMP benchmark." << std::endl;
  7. // Attempt to open the file for writing
  8. std::ofstream fos(file_out);
  9. if (!fos.good()) {
  10. std::cerr << "Could not open file " + file_out << std::endl;
  11. std::exit(-1);
  12. }
  13. // Generate a stringstream to write output to both stdout and a file
  14. std::stringstream ss;
  15.  
  16. // Create a timer used for wall-clock time measurements
  17. Timer t;
  18.  
  19. // Dump a header
  20. generateHeader(ss, {"Experiment", "Matrix size", "Construct (s)", "Randomize (s)"}, {"Float", "Double"}, repeats);
  21. dump(ss, fos, std::cout);
  22.  
  23. // Iterate over each experiment
  24. for (unsigned int e = min; e < max; e++) {
  25. auto mat_dim = 1ul << e;
  26.  
  27. // Print experiment number
  28. ss << std::setw(15) << (std::to_string(e) + ",");
  29.  
  30. // Print the problem size
  31. ss << std::setw(15) << (std::to_string(mat_dim) + ",") << std::flush;
  32.  
  33. // Create the matrices
  34. t.start(); // Start the timer.
  35. auto a = Matrix<float>(mat_dim, mat_dim); // Make a matrix
  36. auto b = Matrix<float>(mat_dim, mat_dim); // And another one, transposed.
  37. auto c = Matrix<double>(mat_dim, mat_dim); // And another one, doubles.
  38. auto d = Matrix<double>(mat_dim, mat_dim); // And another one, transposed.
  39. t.stop(); // Stop the timer.
  40. t.report(ss); // Put interval on stdout
  41.  
  42. // Randomize their contents
  43. t.start();
  44. a.randomize();
  45. b.randomize();
  46. c.randomize();
  47. d.randomize();
  48. t.stop();
  49. t.report(ss);
  50.  
  51. // Dump the initialization output
  52. dump(ss, fos, std::cout);
  53.  
  54. for(unsigned int i = 0; i < repeats; i++){
  55. t.start(); // Start the timer.
  56. auto result = Matrix<float>(a.rows,b.columns);
  57. // Multiply the matrices
  58. result = multiplyMatricesOMP(a, b, threads);
  59. //result.print();
  60. t.stop(); // Stop the timer.
  61. t.report(ss);
  62. // Dump the repeat outcome
  63. dump(ss,fos,std::cout);
  64. }
  65.  
  66. for(unsigned int i = 0; i < repeats; i++){
  67. t.start(); // Start the timer.
  68. auto result = Matrix<double>(c.rows, d.columns);
  69. // Multiply the matrices
  70. result = multiplyMatricesOMP(c, d, threads);
  71. //result.print();
  72. t.stop();
  73. t.report(ss, i+1 == repeats);
  74. // Dump the initialization output
  75. dump(ss,fos,std::cout);
  76. }
  77.  
  78. }
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement