Skip to content

Instantly share code, notes, and snippets.

@cmaureir
Created April 24, 2012 14:08
Show Gist options
  • Save cmaureir/2479973 to your computer and use it in GitHub Desktop.
Save cmaureir/2479973 to your computer and use it in GitHub Desktop.
CUDA Leibniz
#include <stdio.h>
#include <math.h>
#define N (32 * 1000000)
__global__ void pi_elem(float *values)
{
int i = threadIdx.x + blockIdx.x * blockDim.x;
values[i] = pow((float)-1,(float)i);
values[i] /= (2*i+1);
}
int main(int argc, char *argv[]) {
float pi = 0;
int i;
float *d_values, *h_values;
cudaMalloc((void**)&d_values,N*sizeof(float));
h_values = (float*)malloc(N*sizeof(float));
pi_elem <<< N/32, 32 >>>(d_values);
cudaMemcpy(h_values,d_values,N*sizeof(float), cudaMemcpyDeviceToHost);
printf("%f\n", h_values[0]);
for (i = 0; i < N; i++) {
pi += h_values[i];
}
printf("%f\n", 4*pi);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment