-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_loader.cu
176 lines (148 loc) · 3.91 KB
/
data_loader.cu
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <stdio.h>
#include <assert.h>
#include "consts.h"
bool loadData( unsigned x, unsigned y, unsigned z,
char *pDataPath,
float *&deviceData, int *&idData,
float *&hostData, int *&hostIdData)
{
unsigned nTotalCellNum = x * y * z;
float *pCubeCells = new float[nTotalCellNum];
if(pCubeCells == NULL)
{
return false;
}
int *pIdData = new int[nTotalCellNum];
if(pIdData == NULL)
{
return false;
}
/// Info (min, max)
float id_range[4][2] = { { 0xFFFFF, -0xFFFFF },
{ 0xFFFFF, -0xFFFFF },
{ 0xFFFFF, -0xFFFFF },
{ 0xFFFFF, -0xFFFFF } };
/// Loading
///
FILE *fp0 = fopen(pDataPath, "rb");
if(fp0)
{
printf("Loading Data file...\n");
for(int i = 0; i < nTotalCellNum; i ++)
{
fscanf(fp0, "%f %d ", pCubeCells + i, pIdData + i);
// Get Range
int inx = *(pIdData + i) - 1;
float val = *(pCubeCells + i);
if(val < id_range[inx][0]) id_range[inx][0] = val;
if(val > id_range[inx][1]) id_range[inx][1] = val;
//if(inx == -1)
//{
// printf("[%f, %f, %f, %d]\n", *(pCubeCells + 3 * i + 0),
// *(pCubeCells + 3 * i + 1),
// *(pCubeCells + 3 * i + 2),
// *(pIdData + i));
//}
if(i % 10000 == 0)
{
printf("\b\b\b\b\b\b\b\b\b\b%.2f", i * 1.f / nTotalCellNum);
}
}
printf("\nDone\n");
float gRange[2] = {0xFFFFF, -0xFFFFF};
printf("Data Ranges:\n");
for(int i = 0; i < 4; i ++)
{
printf("ID - [%d] : %f -> %f \n", i, id_range[i][0], id_range[i][1]);
gRange[0] = gRange[0] > id_range[i][0] ? id_range[i][0] : gRange[0];
gRange[1] = gRange[1] < id_range[i][1] ? id_range[i][1] : gRange[1];
}
printf("GLOBAL: [%.10f, %.10f]\n", gRange[0], gRange[1]);
fStart = gRange[0];
fEnd = gRange[1];
fclose(fp0);
}
else
{
printf("Opening Data file error... \n Please run DataProcess first\n");
system("pause");
delete [] pIdData;
delete [] pCubeCells;
return false;
}
// Copy to CUDA memory
cudaError_t err = cudaMemcpy( deviceData, pCubeCells, sizeof(float) * nTotalCellNum, cudaMemcpyHostToDevice);
if(err != cudaSuccess)
{
printf("Cell Data Copying Error! \n");
}
err = cudaMemcpy( idData, pIdData, sizeof(int) * nTotalCellNum, cudaMemcpyHostToDevice);
if(err != cudaSuccess)
{
printf("Cell Data Copying Error! \n");
}
//%%% Keep the data for the haptic tool
memcpy(hostData, pCubeCells, sizeof(float) * nTotalCellNum);
memcpy(hostIdData, pIdData, sizeof(int) * nTotalCellNum);
//%%%
delete [] pCubeCells;
delete [] pIdData;
return true;
}
bool loadData2D( unsigned x, unsigned y, unsigned z,
char *pDataPath, float *&deviceData, float *&hostData)
{
unsigned nTotalCellNum = x * y * z;
float *pCubeCells = new float[nTotalCellNum];
if(pCubeCells == NULL)
{
return false;
}
float gRange[2] = {0xFFFFFF, -0xFFFFFF};
/// Loading
///
FILE *fp0 = fopen(pDataPath, "rb");
if(fp0)
{
printf("Loading Data file...\n");
float dmp;
for(int i = 0; i < nTotalCellNum; i ++)
{
fscanf(fp0, "%f %f ", pCubeCells + i, &dmp);
// Get Range
float val = *(pCubeCells + i);
if(val != INVALID_VOLVAL)
{
gRange[1] = val > gRange[1] ? val : gRange[1];
gRange[0] = val < gRange[0] ? val : gRange[0];
}
if(i % VOL_X == 0)
{
printf("\b\b\b\b\b\b\b\b\b\b%.2f", i * 1.f / nTotalCellNum);
}
}
printf("\nDone\n");
printf("GLOBAL: [%.10f, %.10f]\n", gRange[0], gRange[1]);
fStart = gRange[0];
fEnd = gRange[1];
fclose(fp0);
}
else
{
printf("Opening Data file error... \n Please run DataProcess first\n");
system("pause");
delete [] pCubeCells;
return false;
}
// Copy to CUDA memory
cudaError_t err = cudaMemcpy( deviceData, pCubeCells, sizeof(float) * nTotalCellNum, cudaMemcpyHostToDevice);
if(err != cudaSuccess)
{
printf("Cell Data Copying Error! \n");
}
//%%% Keep the data for the haptic tool
memcpy(hostData, pCubeCells, sizeof(float) * nTotalCellNum);
//%%%
delete [] pCubeCells;
return true;
}