-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.h
More file actions
executable file
·88 lines (79 loc) · 2.89 KB
/
utils.h
File metadata and controls
executable file
·88 lines (79 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#ifndef UTILS_H__
#define UTILS_H__
#include <iostream>
#include <iomanip>
#include <cuda.h>
#include <cuda_runtime.h>
#include <cuda_runtime_api.h>
#include <cassert>
#include <cmath>
#define checkCudaErrors(val) check( (val), #val, __FILE__, __LINE__)
template<typename T>
void check(T err, const char* const func, const char* const file, const int line) {
if (err != cudaSuccess) {
std::cerr << "CUDA error at: " << file << ":" << line << std::endl;
std::cerr << cudaGetErrorString(err) << " " << func << std::endl;
exit(1);
}
}
template<typename T>
void checkResultsExact(const T* const ref, const T* const gpu, size_t numElem) {
//check that the GPU result matches the CPU result
for (size_t i = 0; i < numElem; ++i) {
if (ref[i] != gpu[i]) {
std::cerr << "Difference at pos " << i << std::endl;
//the + is magic to convert char to int without messing
//with other types
std::cerr << "Reference: " << std::setprecision(17) << +ref[i] <<
"\nGPU : " << +gpu[i] << std::endl;
exit(1);
}
}
}
template<typename T>
void checkResultsEps(const T* const ref, const T* const gpu, size_t numElem, double eps1, double eps2) {
assert(eps1 >= 0 && eps2 >= 0);
unsigned long long totalDiff = 0;
unsigned numSmallDifferences = 0;
for (size_t i = 0; i < numElem; ++i) {
//subtract smaller from larger in case of unsigned types
T smaller = std::min(ref[i], gpu[i]);
T larger = std::max(ref[i], gpu[i]);
T diff = larger - smaller;
if (diff > 0 && diff <= eps1) {
numSmallDifferences++;
}
else if (diff > eps1) {
std::cerr << "Difference at pos " << +i << " exceeds tolerance of " << eps1 << std::endl;
std::cerr << "Reference: " << std::setprecision(17) << +ref[i] <<
"\nGPU : " << +gpu[i] << std::endl;
exit(1);
}
totalDiff += diff * diff;
}
double percentSmallDifferences = (double)numSmallDifferences / (double)numElem;
if (percentSmallDifferences > eps2) {
std::cerr << "Total percentage of non-zero pixel difference between the two images exceeds " << 100.0 * eps2 << "%" << std::endl;
std::cerr << "Percentage of non-zero pixel differences: " << 100.0 * percentSmallDifferences << "%" << std::endl;
exit(1);
}
}
//Uses the autodesk method of image comparison
//Note the the tolerance here is in PIXELS not a percentage of input pixels
template<typename T>
void checkResultsAutodesk(const T* const ref, const T* const gpu, size_t numElem, double variance, size_t tolerance)
{
size_t numBadPixels = 0;
for (size_t i = 0; i < numElem; ++i) {
T smaller = std::min(ref[i], gpu[i]);
T larger = std::max(ref[i], gpu[i]);
T diff = larger - smaller;
if (diff > variance)
++numBadPixels;
}
if (numBadPixels > tolerance) {
std::cerr << "Too many bad pixels in the image." << numBadPixels << "/" << tolerance << std::endl;
exit(1);
}
}
#endif