Last active
March 26, 2018 08:29
-
-
Save dubik/198475db411f1bcc68397a00f4bee9d6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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