У меня есть изображение с dim.x * dim.y пикселями. Изображение разделено на n_tiles плиток. Каждая плитка содержит несколько записей плитки, которые используются для дальнейших вычислений.
Код: Выделить всё
for (uint32_t x = 0; x < data._dim.x; x++)
{
for (uint32_t y = 0; y < data._dim.y; y++)
{
const uint32_t pix_idx = x + y * data._dim.x;
const uint32_t tile_idx = (x / TILE_SIZE) + (y / TILE_SIZE) * data._tile_dim.x;
const uint2 tile_range = data._tile_ranges[tile_idx];
const uint32_t n_tile_entries = tile_range.y - tile_range.x;
for (uint32_t j = tile_range.x; j < tile_range.y; j++)
{
// perform some calculations + blending
}
img_out[pix_idx] = out_color;
}
}
Моя первая попытка была такая:
Код: Выделить всё
dim3 blockSize(TILE_SIZE * TILE_SIZE); // Number of threads per block -> number of pixels per tile
dim3 gridSize(data._n_tiles); // Number of blocks in grid -> number of total tiles in image
func(data, d_img_out);
Код: Выделить всё
__global__ void func(Data &data, float3* img_out)
{
// calculate indices
const uint32_t tile_idx = blockIdx.x;
const uint32_t pix_idx = ?; // HOW???
// get indices for tile entries
const uint2 tile_range = data._tile_ranges[tile_idx];
const uint32_t n_tile_entries = tile_range.y - tile_range.x;
// number of threads running
const uint32_t number_of_threads = TILE_SIZE * TILE_SIZE;
// let each thread iterate over ceil(n_tile_entries / number_of_threads) in this tile
const uint32_t start = tile_range.x + blockIdx.x * ceil(n_tile_entries / number_of_threads);
const uint32_t end = start + ceil(n_tile_entries / number_of_threads);
for (uint32_t index = start; index < end; index++)
{
// perform some calculations + blending which is another pitfall in parallel....
}
img_out[pix_idx] = out_color;
Подробнее здесь: https://stackoverflow.com/questions/784 ... -into-cuda
Мобильная версия