Spaces:
Sleeping
Sleeping
File size: 7,777 Bytes
66c9c8a | 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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | /** Copyright (c) 2022 NVIDIA CORPORATION. All rights reserved.
* NVIDIA CORPORATION and its licensors retain all intellectual property
* and proprietary rights in and to this software, related documentation
* and any modifications thereto. Any use, reproduction, disclosure or
* distribution of this software and related documentation without an express
* license agreement from NVIDIA CORPORATION is strictly prohibited.
*/
#include "warp.h"
#include "cuda_util.h"
#include "hashgrid.h"
#include "sort.h"
#include "string.h"
using namespace wp;
#include <map>
namespace
{
// host-side copy of mesh descriptors, maps GPU mesh address (id) to a CPU desc
std::map<uint64_t, HashGrid> g_hash_grid_descriptors;
} // anonymous namespace
namespace wp
{
bool hash_grid_get_descriptor(uint64_t id, HashGrid& grid)
{
const auto& iter = g_hash_grid_descriptors.find(id);
if (iter == g_hash_grid_descriptors.end())
return false;
else
grid = iter->second;
return true;
}
void hash_grid_add_descriptor(uint64_t id, const HashGrid& grid)
{
g_hash_grid_descriptors[id] = grid;
}
void hash_grid_rem_descriptor(uint64_t id)
{
g_hash_grid_descriptors.erase(id);
}
// implemented in hashgrid.cu
void hash_grid_rebuild_device(const HashGrid& grid, const wp::vec3* points, int num_points);
} // namespace wp
// host methods
uint64_t hash_grid_create_host(int dim_x, int dim_y, int dim_z)
{
HashGrid* grid = new HashGrid();
memset(grid, 0, sizeof(HashGrid));
grid->dim_x = dim_x;
grid->dim_y = dim_y;
grid->dim_z = dim_z;
const int num_cells = dim_x*dim_y*dim_z;
grid->cell_starts = (int*)alloc_host(num_cells*sizeof(int));
grid->cell_ends = (int*)alloc_host(num_cells*sizeof(int));
return (uint64_t)(grid);
}
void hash_grid_destroy_host(uint64_t id)
{
HashGrid* grid = (HashGrid*)(id);
free_host(grid->point_ids);
free_host(grid->point_cells);
free_host(grid->cell_starts);
free_host(grid->cell_ends);
delete grid;
}
void hash_grid_reserve_host(uint64_t id, int num_points)
{
HashGrid* grid = (HashGrid*)(id);
if (num_points > grid->max_points)
{
free_host(grid->point_cells);
free_host(grid->point_ids);
const int num_to_alloc = num_points*3/2;
grid->point_cells = (int*)alloc_host(2*num_to_alloc*sizeof(int)); // *2 for auxilliary radix buffers
grid->point_ids = (int*)alloc_host(2*num_to_alloc*sizeof(int)); // *2 for auxilliary radix buffers
grid->max_points = num_to_alloc;
}
grid->num_points = num_points;
}
void hash_grid_update_host(uint64_t id, float cell_width, const wp::vec3* points, int num_points)
{
HashGrid* grid = (HashGrid*)(id);
hash_grid_reserve_host(id, num_points);
grid->cell_width = cell_width;
grid->cell_width_inv = 1.0f / cell_width;
// calculate cell for each position
for (int i=0; i < num_points; ++i)
{
grid->point_cells[i] = hash_grid_index(*grid, points[i]);
grid->point_ids[i] = i;
}
// sort indices
radix_sort_pairs_host(grid->point_cells, grid->point_ids, num_points);
const int num_cells = grid->dim_x * grid->dim_y * grid->dim_z;
memset(grid->cell_starts, 0, sizeof(int) * num_cells);
memset(grid->cell_ends, 0, sizeof(int) * num_cells);
// compute cell start / end
for (int i=0; i < num_points; ++i)
{
// scan the particle-cell array to find the start and end
const int c = grid->point_cells[i];
if (i == 0)
grid->cell_starts[c] = 0;
else
{
const int p = grid->point_cells[i-1];
if (c != p)
{
grid->cell_starts[c] = i;
grid->cell_ends[p] = i;
}
}
if (i == num_points - 1)
{
grid->cell_ends[c] = i + 1;
}
}
}
// device methods
uint64_t hash_grid_create_device(void* context, int dim_x, int dim_y, int dim_z)
{
ContextGuard guard(context);
HashGrid grid;
memset(&grid, 0, sizeof(HashGrid));
grid.context = context ? context : cuda_context_get_current();
grid.dim_x = dim_x;
grid.dim_y = dim_y;
grid.dim_z = dim_z;
const int num_cells = dim_x*dim_y*dim_z;
grid.cell_starts = (int*)alloc_device(WP_CURRENT_CONTEXT, num_cells*sizeof(int));
grid.cell_ends = (int*)alloc_device(WP_CURRENT_CONTEXT, num_cells*sizeof(int));
// upload to device
HashGrid* grid_device = (HashGrid*)(alloc_device(WP_CURRENT_CONTEXT, sizeof(HashGrid)));
memcpy_h2d(WP_CURRENT_CONTEXT, grid_device, &grid, sizeof(HashGrid));
uint64_t grid_id = (uint64_t)(grid_device);
hash_grid_add_descriptor(grid_id, grid);
return grid_id;
}
void hash_grid_destroy_device(uint64_t id)
{
HashGrid grid;
if (hash_grid_get_descriptor(id, grid))
{
ContextGuard guard(grid.context);
free_device(WP_CURRENT_CONTEXT, grid.point_ids);
free_device(WP_CURRENT_CONTEXT, grid.point_cells);
free_device(WP_CURRENT_CONTEXT, grid.cell_starts);
free_device(WP_CURRENT_CONTEXT, grid.cell_ends);
free_device(WP_CURRENT_CONTEXT, (HashGrid*)id);
hash_grid_rem_descriptor(id);
}
}
void hash_grid_reserve_device(uint64_t id, int num_points)
{
HashGrid grid;
if (hash_grid_get_descriptor(id, grid))
{
if (num_points > grid.max_points)
{
ContextGuard guard(grid.context);
free_device(WP_CURRENT_CONTEXT, grid.point_cells);
free_device(WP_CURRENT_CONTEXT, grid.point_ids);
const int num_to_alloc = num_points*3/2;
grid.point_cells = (int*)alloc_device(WP_CURRENT_CONTEXT, 2*num_to_alloc*sizeof(int)); // *2 for auxilliary radix buffers
grid.point_ids = (int*)alloc_device(WP_CURRENT_CONTEXT, 2*num_to_alloc*sizeof(int)); // *2 for auxilliary radix buffers
grid.max_points = num_to_alloc;
// ensure we pre-size our sort routine to avoid
// allocations during graph capture
radix_sort_reserve(WP_CURRENT_CONTEXT, num_to_alloc);
// update device side grid descriptor, todo: this is
// slightly redundant since it is performed again
// inside hash_grid_update_device(), but since
// reserve can be called from Python we need to make
// sure it is consistent
memcpy_h2d(WP_CURRENT_CONTEXT, (HashGrid*)id, &grid, sizeof(HashGrid));
// update host side grid descriptor
hash_grid_add_descriptor(id, grid);
}
}
}
void hash_grid_update_device(uint64_t id, float cell_width, const wp::vec3* points, int num_points)
{
// ensure we have enough memory reserved for update
// this must be done before retrieving the descriptor
// below since it may update it
hash_grid_reserve_device(id, num_points);
// host grid must be static so that we can
// perform host->device memcpy from this variable
// and have it safely recorded inside CUDA graphs
static HashGrid grid;
if (hash_grid_get_descriptor(id, grid))
{
ContextGuard guard(grid.context);
grid.num_points = num_points;
grid.cell_width = cell_width;
grid.cell_width_inv = 1.0f / cell_width;
hash_grid_rebuild_device(grid, points, num_points);
// update device side grid descriptor
memcpy_h2d(WP_CURRENT_CONTEXT, (HashGrid*)id, &grid, sizeof(HashGrid));
// update host side grid descriptor
hash_grid_add_descriptor(id, grid);
}
}
#if !WP_ENABLE_CUDA
namespace wp
{
void hash_grid_rebuild_device(const HashGrid& grid, const wp::vec3* points, int num_points)
{
}
} // namespace wp
#endif // !WP_ENABLE_CUDA |