util.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #ifndef _C_UTIL_
  2. #define _C_UTIL_
  3. #include <math.h>
  4. #include <iostream>
  5. #include <omp.h>
  6. //-------------------------------------------------------------------
  7. //--initialize array with maximum limit
  8. //-------------------------------------------------------------------
  9. template<typename datatype>
  10. void fill(datatype *A, const int n, const datatype maxi){
  11. for (int j = 0; j < n; j++)
  12. {
  13. A[j] = ((datatype) maxi * (rand() / (RAND_MAX + 1.0f)));
  14. }
  15. }
  16. //--print matrix
  17. template<typename datatype>
  18. void print_matrix(datatype *A, int height, int width){
  19. for(int i=0; i<height; i++){
  20. for(int j=0; j<width; j++){
  21. int idx = i*width + j;
  22. std::cout<<A[idx]<<" ";
  23. }
  24. std::cout<<std::endl;
  25. }
  26. return;
  27. }
  28. //-------------------------------------------------------------------
  29. //--verify results
  30. //-------------------------------------------------------------------
  31. #define MAX_RELATIVE_ERROR .002
  32. template<typename datatype>
  33. void verify_array(const datatype *cpuResults, const datatype *gpuResults, const int size){
  34. char passed = true;
  35. #pragma omp parallel for
  36. for (int i=0; i<size; i++){
  37. if (fabs(cpuResults[i] - gpuResults[i]) / cpuResults[i] > MAX_RELATIVE_ERROR){
  38. passed = false;
  39. }
  40. }
  41. if (passed){
  42. std::cout << "--cambine:passed:-)" << endl;
  43. }
  44. else{
  45. std::cout << "--cambine: failed:-(" << endl;
  46. }
  47. return ;
  48. }
  49. template<typename datatype>
  50. void compare_results(const datatype *cpu_results, const datatype *gpu_results, const int size){
  51. char passed = true;
  52. //#pragma omp parallel for
  53. for (int i=0; i<size; i++){
  54. if (cpu_results[i]!=gpu_results[i]){
  55. passed = false;
  56. }
  57. }
  58. if (passed){
  59. std::cout << "--cambine:passed:-)" << endl;
  60. }
  61. else{
  62. std::cout << "--cambine: failed:-(" << endl;
  63. }
  64. return ;
  65. }
  66. #endif