-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug.cpp
70 lines (60 loc) · 2.41 KB
/
debug.cpp
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
#include <iostream>
#include <fstream>
#include <cstdint>
#ifdef AFET_CUDA
#include <cuda_runtime.h>
#endif
#define EXTERN
#include "debug.h"
void export_c_buffer(void * data, size_t width, size_t height, size_t elemsize, std::string filename)
{
std::cout << "[Debug] Exporting C buffer of size " << width << " x " << height << " x " << elemsize << " B to file '" << filename.c_str() << "'\n";
size_t datasize = width * height * elemsize;
uint32_t w = width,
h = height,
e = elemsize;
std::ofstream fout(filename.c_str(), std::ios::out | std::ios::binary);
fout.write((const char *)&w, sizeof(w));
fout.write((const char *)&h, sizeof(h));
fout.write((const char *)&e, sizeof(e));
fout.write((const char *)data, datasize);
fout.close();
}
#ifdef AFET_CUDA
void export_cuda_buffer(void * d_data, size_t width, size_t height, size_t elemsize, std::string filename)
{
std::cout << "[Debug] Exporting CUDA buffer of size " << width << " x " << height << " x " << elemsize << " B to file '" << filename.c_str() << "'\n";
size_t datasize = width * height * elemsize;
char * data = new char [datasize];
cudaMemcpy(data, d_data, datasize, cudaMemcpyDeviceToHost);
uint32_t w = width,
h = height,
e = elemsize;
std::ofstream fout(filename.c_str(), std::ios::out | std::ios::binary);
fout.write((const char *)&w, sizeof(w));
fout.write((const char *)&h, sizeof(h));
fout.write((const char *)&e, sizeof(e));
fout.write((const char *)data, datasize);
fout.close();
delete [] data;
}
#endif
#ifdef AFET_OPENCL
void export_cl_buffer(cl_command_queue cmd_queue, cl_mem d_data, size_t width, size_t height, size_t elemsize, std::string filename)
{
std::cout << "[Debug] Exporting OpenCL buffer of size " << width << " x " << height << " x " << elemsize << " B to file '" << filename.c_str() << "'\n";
size_t datasize = width * height * elemsize;
char * data = new char [datasize];
clEnqueueReadBuffer(cmd_queue, d_data, CL_TRUE, 0, datasize, data, 0, NULL, NULL);
uint32_t w = width,
h = height,
e = elemsize;
std::ofstream fout(filename.c_str(), std::ios::out | std::ios::binary);
fout.write((const char *)&w, sizeof(w));
fout.write((const char *)&h, sizeof(h));
fout.write((const char *)&e, sizeof(e));
fout.write((const char *)data, datasize);
fout.close();
delete [] data;
}
#endif