mirror of
https://github.com/NVIDIA/cuda-samples.git
synced 2026-10-25 22:28:17 +08:00
Apply consistent code formatting across the repo. Add clang-format and pre-commit hooks.
This commit is contained in:
@@ -36,71 +36,72 @@ namespace cg = cooperative_groups;
|
||||
|
||||
// Use power method to find the first eigenvector.
|
||||
// https://en.wikipedia.org/wiki/Power_iteration
|
||||
inline __device__ __host__ float3 firstEigenVector(float matrix[6]) {
|
||||
// 8 iterations seems to be more than enough.
|
||||
inline __device__ __host__ float3 firstEigenVector(float matrix[6])
|
||||
{
|
||||
// 8 iterations seems to be more than enough.
|
||||
|
||||
float3 v = make_float3(1.0f, 1.0f, 1.0f);
|
||||
float3 v = make_float3(1.0f, 1.0f, 1.0f);
|
||||
|
||||
for (int i = 0; i < 8; i++) {
|
||||
float x = v.x * matrix[0] + v.y * matrix[1] + v.z * matrix[2];
|
||||
float y = v.x * matrix[1] + v.y * matrix[3] + v.z * matrix[4];
|
||||
float z = v.x * matrix[2] + v.y * matrix[4] + v.z * matrix[5];
|
||||
float m = max(max(x, y), z);
|
||||
float iv = 1.0f / m;
|
||||
v = make_float3(x * iv, y * iv, z * iv);
|
||||
}
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
inline __device__ void colorSums(const float3 *colors, float3 *sums,
|
||||
cg::thread_group tile) {
|
||||
const int idx = threadIdx.x;
|
||||
|
||||
sums[idx] = colors[idx];
|
||||
cg::sync(tile);
|
||||
sums[idx] += sums[idx ^ 8];
|
||||
cg::sync(tile);
|
||||
sums[idx] += sums[idx ^ 4];
|
||||
cg::sync(tile);
|
||||
sums[idx] += sums[idx ^ 2];
|
||||
cg::sync(tile);
|
||||
sums[idx] += sums[idx ^ 1];
|
||||
}
|
||||
|
||||
inline __device__ float3 bestFitLine(const float3 *colors, float3 color_sum,
|
||||
cg::thread_group tile) {
|
||||
// Compute covariance matrix of the given colors.
|
||||
const int idx = threadIdx.x;
|
||||
|
||||
float3 diff = colors[idx] - color_sum * (1.0f / 16.0f);
|
||||
|
||||
// @@ Eliminate two-way bank conflicts here.
|
||||
// @@ It seems that doing that and unrolling the reduction doesn't help...
|
||||
__shared__ float covariance[16 * 6];
|
||||
|
||||
covariance[6 * idx + 0] = diff.x * diff.x; // 0, 6, 12, 2, 8, 14, 4, 10, 0
|
||||
covariance[6 * idx + 1] = diff.x * diff.y;
|
||||
covariance[6 * idx + 2] = diff.x * diff.z;
|
||||
covariance[6 * idx + 3] = diff.y * diff.y;
|
||||
covariance[6 * idx + 4] = diff.y * diff.z;
|
||||
covariance[6 * idx + 5] = diff.z * diff.z;
|
||||
|
||||
cg::sync(tile);
|
||||
for (int d = 8; d > 0; d >>= 1) {
|
||||
if (idx < d) {
|
||||
covariance[6 * idx + 0] += covariance[6 * (idx + d) + 0];
|
||||
covariance[6 * idx + 1] += covariance[6 * (idx + d) + 1];
|
||||
covariance[6 * idx + 2] += covariance[6 * (idx + d) + 2];
|
||||
covariance[6 * idx + 3] += covariance[6 * (idx + d) + 3];
|
||||
covariance[6 * idx + 4] += covariance[6 * (idx + d) + 4];
|
||||
covariance[6 * idx + 5] += covariance[6 * (idx + d) + 5];
|
||||
for (int i = 0; i < 8; i++) {
|
||||
float x = v.x * matrix[0] + v.y * matrix[1] + v.z * matrix[2];
|
||||
float y = v.x * matrix[1] + v.y * matrix[3] + v.z * matrix[4];
|
||||
float z = v.x * matrix[2] + v.y * matrix[4] + v.z * matrix[5];
|
||||
float m = max(max(x, y), z);
|
||||
float iv = 1.0f / m;
|
||||
v = make_float3(x * iv, y * iv, z * iv);
|
||||
}
|
||||
cg::sync(tile);
|
||||
}
|
||||
|
||||
// Compute first eigen vector.
|
||||
return firstEigenVector(covariance);
|
||||
return v;
|
||||
}
|
||||
|
||||
#endif // CUDAMATH_H
|
||||
inline __device__ void colorSums(const float3 *colors, float3 *sums, cg::thread_group tile)
|
||||
{
|
||||
const int idx = threadIdx.x;
|
||||
|
||||
sums[idx] = colors[idx];
|
||||
cg::sync(tile);
|
||||
sums[idx] += sums[idx ^ 8];
|
||||
cg::sync(tile);
|
||||
sums[idx] += sums[idx ^ 4];
|
||||
cg::sync(tile);
|
||||
sums[idx] += sums[idx ^ 2];
|
||||
cg::sync(tile);
|
||||
sums[idx] += sums[idx ^ 1];
|
||||
}
|
||||
|
||||
inline __device__ float3 bestFitLine(const float3 *colors, float3 color_sum, cg::thread_group tile)
|
||||
{
|
||||
// Compute covariance matrix of the given colors.
|
||||
const int idx = threadIdx.x;
|
||||
|
||||
float3 diff = colors[idx] - color_sum * (1.0f / 16.0f);
|
||||
|
||||
// @@ Eliminate two-way bank conflicts here.
|
||||
// @@ It seems that doing that and unrolling the reduction doesn't help...
|
||||
__shared__ float covariance[16 * 6];
|
||||
|
||||
covariance[6 * idx + 0] = diff.x * diff.x; // 0, 6, 12, 2, 8, 14, 4, 10, 0
|
||||
covariance[6 * idx + 1] = diff.x * diff.y;
|
||||
covariance[6 * idx + 2] = diff.x * diff.z;
|
||||
covariance[6 * idx + 3] = diff.y * diff.y;
|
||||
covariance[6 * idx + 4] = diff.y * diff.z;
|
||||
covariance[6 * idx + 5] = diff.z * diff.z;
|
||||
|
||||
cg::sync(tile);
|
||||
for (int d = 8; d > 0; d >>= 1) {
|
||||
if (idx < d) {
|
||||
covariance[6 * idx + 0] += covariance[6 * (idx + d) + 0];
|
||||
covariance[6 * idx + 1] += covariance[6 * (idx + d) + 1];
|
||||
covariance[6 * idx + 2] += covariance[6 * (idx + d) + 2];
|
||||
covariance[6 * idx + 3] += covariance[6 * (idx + d) + 3];
|
||||
covariance[6 * idx + 4] += covariance[6 * (idx + d) + 4];
|
||||
covariance[6 * idx + 5] += covariance[6 * (idx + d) + 5];
|
||||
}
|
||||
cg::sync(tile);
|
||||
}
|
||||
|
||||
// Compute first eigen vector.
|
||||
return firstEigenVector(covariance);
|
||||
}
|
||||
|
||||
#endif // CUDAMATH_H
|
||||
|
||||
@@ -29,56 +29,58 @@
|
||||
#define DDS_H
|
||||
|
||||
#if !defined(MAKEFOURCC)
|
||||
#define MAKEFOURCC(ch0, ch1, ch2, ch3) \
|
||||
((unsigned int)(ch0) | ((unsigned int)(ch1) << 8) | \
|
||||
((unsigned int)(ch2) << 16) | ((unsigned int)(ch3) << 24))
|
||||
#define MAKEFOURCC(ch0, ch1, ch2, ch3) \
|
||||
((unsigned int)(ch0) | ((unsigned int)(ch1) << 8) | ((unsigned int)(ch2) << 16) | ((unsigned int)(ch3) << 24))
|
||||
#endif
|
||||
|
||||
typedef unsigned int uint;
|
||||
typedef unsigned int uint;
|
||||
typedef unsigned short ushort;
|
||||
|
||||
struct DDSPixelFormat {
|
||||
uint size;
|
||||
uint flags;
|
||||
uint fourcc;
|
||||
uint bitcount;
|
||||
uint rmask;
|
||||
uint gmask;
|
||||
uint bmask;
|
||||
uint amask;
|
||||
struct DDSPixelFormat
|
||||
{
|
||||
uint size;
|
||||
uint flags;
|
||||
uint fourcc;
|
||||
uint bitcount;
|
||||
uint rmask;
|
||||
uint gmask;
|
||||
uint bmask;
|
||||
uint amask;
|
||||
};
|
||||
|
||||
struct DDSCaps {
|
||||
uint caps1;
|
||||
uint caps2;
|
||||
uint caps3;
|
||||
uint caps4;
|
||||
struct DDSCaps
|
||||
{
|
||||
uint caps1;
|
||||
uint caps2;
|
||||
uint caps3;
|
||||
uint caps4;
|
||||
};
|
||||
|
||||
/// DDS file header.
|
||||
struct DDSHeader {
|
||||
uint fourcc;
|
||||
uint size;
|
||||
uint flags;
|
||||
uint height;
|
||||
uint width;
|
||||
uint pitch;
|
||||
uint depth;
|
||||
uint mipmapcount;
|
||||
uint reserved[11];
|
||||
DDSPixelFormat pf;
|
||||
DDSCaps caps;
|
||||
uint notused;
|
||||
struct DDSHeader
|
||||
{
|
||||
uint fourcc;
|
||||
uint size;
|
||||
uint flags;
|
||||
uint height;
|
||||
uint width;
|
||||
uint pitch;
|
||||
uint depth;
|
||||
uint mipmapcount;
|
||||
uint reserved[11];
|
||||
DDSPixelFormat pf;
|
||||
DDSCaps caps;
|
||||
uint notused;
|
||||
};
|
||||
|
||||
static const uint FOURCC_DDS = MAKEFOURCC('D', 'D', 'S', ' ');
|
||||
static const uint FOURCC_DXT1 = MAKEFOURCC('D', 'X', 'T', '1');
|
||||
static const uint DDSD_WIDTH = 0x00000004U;
|
||||
static const uint DDSD_HEIGHT = 0x00000002U;
|
||||
static const uint DDSD_CAPS = 0x00000001U;
|
||||
static const uint FOURCC_DDS = MAKEFOURCC('D', 'D', 'S', ' ');
|
||||
static const uint FOURCC_DXT1 = MAKEFOURCC('D', 'X', 'T', '1');
|
||||
static const uint DDSD_WIDTH = 0x00000004U;
|
||||
static const uint DDSD_HEIGHT = 0x00000002U;
|
||||
static const uint DDSD_CAPS = 0x00000001U;
|
||||
static const uint DDSD_PIXELFORMAT = 0x00001000U;
|
||||
static const uint DDSCAPS_TEXTURE = 0x00001000U;
|
||||
static const uint DDPF_FOURCC = 0x00000004U;
|
||||
static const uint DDSD_LINEARSIZE = 0x00080000U;
|
||||
static const uint DDSCAPS_TEXTURE = 0x00001000U;
|
||||
static const uint DDPF_FOURCC = 0x00000004U;
|
||||
static const uint DDSD_LINEARSIZE = 0x00080000U;
|
||||
|
||||
#endif // DDS_H
|
||||
#endif // DDS_H
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -28,116 +28,118 @@
|
||||
#ifndef PERMUTATIONS_H
|
||||
#define PERMUTATIONS_H
|
||||
|
||||
#include <helper_cuda.h> // assert
|
||||
#include <helper_cuda.h> // assert
|
||||
|
||||
static void computePermutations(uint permutations[1024]) {
|
||||
int indices[16];
|
||||
int num = 0;
|
||||
static void computePermutations(uint permutations[1024])
|
||||
{
|
||||
int indices[16];
|
||||
int num = 0;
|
||||
|
||||
// 3 element permutations:
|
||||
// 3 element permutations:
|
||||
|
||||
// first cluster [0,i) is at the start
|
||||
for (int m = 0; m < 16; ++m) {
|
||||
indices[m] = 0;
|
||||
}
|
||||
|
||||
const int imax = 15;
|
||||
|
||||
for (int i = imax; i >= 0; --i) {
|
||||
// second cluster [i,j) is half along
|
||||
for (int m = i; m < 16; ++m) {
|
||||
indices[m] = 2;
|
||||
// first cluster [0,i) is at the start
|
||||
for (int m = 0; m < 16; ++m) {
|
||||
indices[m] = 0;
|
||||
}
|
||||
|
||||
const int jmax = (i == 0) ? 15 : 16;
|
||||
const int imax = 15;
|
||||
|
||||
for (int j = jmax; j >= i; --j) {
|
||||
// last cluster [j,k) is at the end
|
||||
if (j < 16) {
|
||||
indices[j] = 1;
|
||||
}
|
||||
|
||||
uint permutation = 0;
|
||||
|
||||
for (int p = 0; p < 16; p++) {
|
||||
permutation |= indices[p] << (p * 2);
|
||||
// permutation |= indices[15-p] << (p * 2);
|
||||
}
|
||||
|
||||
permutations[num] = permutation;
|
||||
|
||||
num++;
|
||||
}
|
||||
}
|
||||
|
||||
assert(num == 151);
|
||||
|
||||
for (int i = 0; i < 9; i++) {
|
||||
permutations[num] = 0x000AA555;
|
||||
num++;
|
||||
}
|
||||
|
||||
assert(num == 160);
|
||||
|
||||
// Append 4 element permutations:
|
||||
|
||||
// first cluster [0,i) is at the start
|
||||
for (int m = 0; m < 16; ++m) {
|
||||
indices[m] = 0;
|
||||
}
|
||||
|
||||
for (int i = imax; i >= 0; --i) {
|
||||
// second cluster [i,j) is one third along
|
||||
for (int m = i; m < 16; ++m) {
|
||||
indices[m] = 2;
|
||||
}
|
||||
|
||||
const int jmax = (i == 0) ? 15 : 16;
|
||||
|
||||
for (int j = jmax; j >= i; --j) {
|
||||
// third cluster [j,k) is two thirds along
|
||||
for (int m = j; m < 16; ++m) {
|
||||
indices[m] = 3;
|
||||
}
|
||||
|
||||
int kmax = (j == 0) ? 15 : 16;
|
||||
|
||||
for (int k = kmax; k >= j; --k) {
|
||||
// last cluster [k,n) is at the end
|
||||
if (k < 16) {
|
||||
indices[k] = 1;
|
||||
for (int i = imax; i >= 0; --i) {
|
||||
// second cluster [i,j) is half along
|
||||
for (int m = i; m < 16; ++m) {
|
||||
indices[m] = 2;
|
||||
}
|
||||
|
||||
uint permutation = 0;
|
||||
const int jmax = (i == 0) ? 15 : 16;
|
||||
|
||||
bool hasThree = false;
|
||||
for (int j = jmax; j >= i; --j) {
|
||||
// last cluster [j,k) is at the end
|
||||
if (j < 16) {
|
||||
indices[j] = 1;
|
||||
}
|
||||
|
||||
for (int p = 0; p < 16; p++) {
|
||||
permutation |= indices[p] << (p * 2);
|
||||
// permutation |= indices[15-p] << (p * 2);
|
||||
uint permutation = 0;
|
||||
|
||||
if (indices[p] == 3) hasThree = true;
|
||||
for (int p = 0; p < 16; p++) {
|
||||
permutation |= indices[p] << (p * 2);
|
||||
// permutation |= indices[15-p] << (p * 2);
|
||||
}
|
||||
|
||||
permutations[num] = permutation;
|
||||
|
||||
num++;
|
||||
}
|
||||
|
||||
if (hasThree) {
|
||||
permutations[num] = permutation;
|
||||
num++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assert(num == 975);
|
||||
assert(num == 151);
|
||||
|
||||
// 1024 - 969 - 7 = 48 extra elements
|
||||
for (int i = 0; i < 9; i++) {
|
||||
permutations[num] = 0x000AA555;
|
||||
num++;
|
||||
}
|
||||
|
||||
// It would be nice to set these extra elements with better values...
|
||||
for (int i = 0; i < 49; i++) {
|
||||
permutations[num] = 0x00AAFF55;
|
||||
num++;
|
||||
}
|
||||
assert(num == 160);
|
||||
|
||||
assert(num == 1024);
|
||||
// Append 4 element permutations:
|
||||
|
||||
// first cluster [0,i) is at the start
|
||||
for (int m = 0; m < 16; ++m) {
|
||||
indices[m] = 0;
|
||||
}
|
||||
|
||||
for (int i = imax; i >= 0; --i) {
|
||||
// second cluster [i,j) is one third along
|
||||
for (int m = i; m < 16; ++m) {
|
||||
indices[m] = 2;
|
||||
}
|
||||
|
||||
const int jmax = (i == 0) ? 15 : 16;
|
||||
|
||||
for (int j = jmax; j >= i; --j) {
|
||||
// third cluster [j,k) is two thirds along
|
||||
for (int m = j; m < 16; ++m) {
|
||||
indices[m] = 3;
|
||||
}
|
||||
|
||||
int kmax = (j == 0) ? 15 : 16;
|
||||
|
||||
for (int k = kmax; k >= j; --k) {
|
||||
// last cluster [k,n) is at the end
|
||||
if (k < 16) {
|
||||
indices[k] = 1;
|
||||
}
|
||||
|
||||
uint permutation = 0;
|
||||
|
||||
bool hasThree = false;
|
||||
|
||||
for (int p = 0; p < 16; p++) {
|
||||
permutation |= indices[p] << (p * 2);
|
||||
// permutation |= indices[15-p] << (p * 2);
|
||||
|
||||
if (indices[p] == 3)
|
||||
hasThree = true;
|
||||
}
|
||||
|
||||
if (hasThree) {
|
||||
permutations[num] = permutation;
|
||||
num++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assert(num == 975);
|
||||
|
||||
// 1024 - 969 - 7 = 48 extra elements
|
||||
|
||||
// It would be nice to set these extra elements with better values...
|
||||
for (int i = 0; i < 49; i++) {
|
||||
permutations[num] = 0x00AAFF55;
|
||||
num++;
|
||||
}
|
||||
|
||||
assert(num == 1024);
|
||||
}
|
||||
|
||||
#endif // PERMUTATIONS_H
|
||||
#endif // PERMUTATIONS_H
|
||||
|
||||
Reference in New Issue
Block a user