#define CUB_STDERR
#include <stdio.h>
#include <limits>
#include <cub/util_allocator.cuh>
#include "../../test/test_util.h"
using namespace cub;
bool g_verbose = false;
template <
int BINS,
int CHANNELS,
int ACTIVE_CHANNELS>
void Initialize(
unsigned char *h_samples,
int h_histograms[ACTIVE_CHANNELS][BINS],
int num_pixels)
{
for (int channel = 0; channel < ACTIVE_CHANNELS; ++channel)
for (int bin = 0; bin < BINS; ++bin)
h_histograms[channel][bin] = 0;
if (g_verbose) printf("Samples: \n");
for (int i = 0; i < num_pixels * CHANNELS; i += CHANNELS)
{
if (g_verbose) std::cout << "<";
for (int channel = 0; channel < ACTIVE_CHANNELS; ++channel)
{
RandomBits(h_samples[i + channel]);
h_histograms[channel][h_samples[i + channel]]++;
if (g_verbose) {
std::cout << int(h_samples[i + channel]);
if (channel < CHANNELS - 1) std::cout << ",";
}
}
for (int channel = ACTIVE_CHANNELS; channel < CHANNELS; ++channel)
{
RandomBits(h_samples[i + channel]);
if (g_verbose) {
std::cout << int(h_samples[i + channel]);
if (channel < CHANNELS - 1) std::cout << ",";
}
}
if (g_verbose) std::cout << "> ";
}
if (g_verbose) printf("\n\n");
}
int main(int argc, char** argv)
{
const int CHANNELS = 4;
const int ACTIVE_CHANNELS = 3;
const int BINS = 256;
int num_pixels = 150;
CommandLineArgs args(argc, argv);
g_verbose = args.CheckCmdLineFlag("v");
bool quick = args.CheckCmdLineFlag("quick");
args.GetCmdLineArgument("n", num_pixels);
if (args.CheckCmdLineFlag("help"))
{
printf("%s "
"[--n=<number of RGBA pixels> "
"[--device=<device-id>] "
"[--v] "
"\n", argv[0]);
exit(0);
}
int num_samples = num_pixels * CHANNELS;
printf("cub::DeviceHistogram::MultiChannelSharedAtomic() RGB histograms from %d RGBA pixels (%d %d-byte channel samples)\n",
num_pixels, num_samples, (int) sizeof(unsigned char));
fflush(stdout);
int total_bins = ACTIVE_CHANNELS * BINS;
unsigned char *h_samples = new unsigned char[num_samples];
int h_histograms[ACTIVE_CHANNELS][BINS];
Initialize<BINS, CHANNELS, ACTIVE_CHANNELS>(h_samples, h_histograms, num_pixels);
unsigned char *d_samples = NULL;
int *d_histograms_linear = NULL;
CubDebugExit(g_allocator.DeviceAllocate((
void**)&d_samples,
sizeof(
unsigned char) * num_samples));
CubDebugExit(g_allocator.DeviceAllocate((
void**)&d_histograms_linear,
sizeof(
int) * total_bins));
CubDebugExit(cudaMemcpy(d_samples, h_samples,
sizeof(
unsigned char) * num_samples, cudaMemcpyHostToDevice));
int *d_histograms[ACTIVE_CHANNELS];
for (int CHANNEL = 0; CHANNEL < ACTIVE_CHANNELS; ++CHANNEL)
d_histograms[CHANNEL] = d_histograms_linear + (CHANNEL * BINS);
void *d_temp_storage = NULL;
size_t temp_storage_bytes = 0;
CubDebugExit((DeviceHistogram::MultiChannelSharedAtomic<BINS, CHANNELS, ACTIVE_CHANNELS>(d_temp_storage, temp_storage_bytes, d_sample_itr, d_histograms, num_samples)));
CubDebugExit(g_allocator.DeviceAllocate(&d_temp_storage, temp_storage_bytes));
CubDebugExit((DeviceHistogram::MultiChannelSharedAtomic<BINS, CHANNELS, ACTIVE_CHANNELS>(d_temp_storage, temp_storage_bytes, d_sample_itr, d_histograms, num_samples)));
printf("R channel: ");
int compare = CompareDeviceResults(h_histograms[0], d_histograms[0], BINS, g_verbose, g_verbose);
printf("%s\n", compare ? "FAIL" : "PASS");
AssertEquals(0, compare);
printf("G channel: ");
compare = CompareDeviceResults(h_histograms[1], d_histograms[1], BINS, g_verbose, g_verbose);
printf("%s\n", compare ? "FAIL" : "PASS");
AssertEquals(0, compare);
printf("B channel: ");
compare = CompareDeviceResults(h_histograms[2], d_histograms[2], BINS, g_verbose, g_verbose);
printf("%s\n", compare ? "FAIL" : "PASS");
AssertEquals(0, compare);
if (h_samples) delete[] h_samples;
if (d_samples)
CubDebugExit(g_allocator.DeviceFree(d_samples));
if (d_histograms_linear)
CubDebugExit(g_allocator.DeviceFree(d_histograms_linear));
if (d_temp_storage)
CubDebugExit(g_allocator.DeviceFree(d_temp_storage));
printf("\n\n");
return 0;
}