util.h 2.0 KB

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