Skip to content

Instantly share code, notes, and snippets.

@dubik
Last active March 26, 2018 08:29
Show Gist options
  • Save dubik/198475db411f1bcc68397a00f4bee9d6 to your computer and use it in GitHub Desktop.
Save dubik/198475db411f1bcc68397a00f4bee9d6 to your computer and use it in GitHub Desktop.
const int N_VECTORS = 1<<20;
const int VECTOR_COMPONENTS = 512;
const int KBlockSize = 256;
__global__
void calculateDistances(const float v[VECTOR_COMPONENTS], float *vectors, float *distances)
{
int vectorIndex = threadIdx.x + threadIdx.y * KBlockSize;
if (vectorIndex < N_VECTORS) {
float *vector = vectors + vectorIndex * VECTOR_COMPONENTS;
float sum = 0.0f;
for (int i = 0; i < VECTOR_COMPONENTS; i++) {
float a = vector[i];
float b = v[i];
float diff = a - b;
sum += diff * diff;
}
distances[vectorIndex] = sqrtf(sum);
}
}
int main()
{
...
float v[VECTOR_COMPONENTS];
cudaMallocManaged(&vectors, sizeof(float) * N_VECTORS * VECTOR_COMPONENTS);
cudaMallocManaged(&distance, sizeof(float) * N_VECTORS);
calculateDistance<<<N_VECTORS/KBlockSize + 1, KBlockSize>>>(v, vectors, distance);
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment