#include <iostream>
#include <cuda.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
using namespace std;

__global__ void kernel_gpu(int *A0,float *B0,float *B1,float *B2,float *B3,int N){
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < N){
A0[i] = A0[i];
B0[i] = B3[i]+B2[i]-B3[i];
B1[i] = B2[i]+B3[i]/B2[i];
}
}

void kernel_cpu(int *A0,float *B0,float *B1,float *B2,float *B3,int N){
for(int i=0;i<N;i++){
A0[i] = A0[i];
B0[i] = B3[i]+B2[i]-B3[i];
B1[i] = B2[i]+B3[i]/B2[i];
}
}

int main(int argc,char **argv) {
fprintf(stderr, "68 ");
int size=93185;
int intBytes = size*sizeof(int);
int floatBytes = size*sizeof(float);
int *A0;
A0 = (int *)malloc(intBytes);
float *B0;
B0 = (float *)malloc(floatBytes);
float *B1;
B1 = (float *)malloc(floatBytes);
float *B2;
B2 = (float *)malloc(floatBytes);
float *B3;
B3 = (float *)malloc(floatBytes);
for(int i=0;i<93185;i++){
A0[i] = 29*i+1;
B0[i] = 99.073654618*i+1;
B1[i] = 0.891724839412*i+1;
B2[i] = 89.8685157634*i+1;
B3[i] = 14.6077037294*i+1;
}
int *d_A0;
cudaMalloc((void **)&d_A0,93185*sizeof(int));
cudaMemcpy(d_A0,A0,93185*sizeof(int),cudaMemcpyHostToDevice);
float *d_B0;
cudaMalloc((void **)&d_B0,93185*sizeof(float));
cudaMemcpy(d_B0,B0,93185*sizeof(float),cudaMemcpyHostToDevice);
float *d_B1;
cudaMalloc((void **)&d_B1,93185*sizeof(float));
cudaMemcpy(d_B1,B1,93185*sizeof(float),cudaMemcpyHostToDevice);
float *d_B2;
cudaMalloc((void **)&d_B2,93185*sizeof(float));
cudaMemcpy(d_B2,B2,93185*sizeof(float),cudaMemcpyHostToDevice);
float *d_B3;
cudaMalloc((void **)&d_B3,93185*sizeof(float));
cudaMemcpy(d_B3,B3,93185*sizeof(float),cudaMemcpyHostToDevice);
int n = 365*256;
int block_size = 256;
int block_no = n/block_size;
struct timeval time0,time1;
gettimeofday(&time0,NULL);
kernel_gpu<<<block_no,block_size>>>(d_A0,d_B0,d_B1,d_B2,d_B3,93185);
cudaThreadSynchronize();
gettimeofday(&time1,NULL);
double totaltime10 = (time1.tv_sec*1000000.0 + time1.tv_usec) - (time0.tv_sec*1000000.0 + time0.tv_usec);
fprintf(stderr, "GPU time: %lf msecs ",  (totaltime10)/1000.0F);
gettimeofday(&time0,NULL);
kernel_cpu(A0,B0,B1,B2,B3,93185);
gettimeofday(&time1,NULL);
totaltime10 = (time1.tv_sec*1000000.0 + time1.tv_usec) - (time0.tv_sec*1000000.0 + time0.tv_usec);
fprintf(stderr, "CPU time: %lf msecs ",  (totaltime10)/1000.0F);
int *testA0;
testA0 = (int *)malloc(intBytes);
cudaMemcpy(testA0,d_A0,93185*sizeof(int),cudaMemcpyDeviceToHost);
float *testB0;
testB0 = (float *)malloc(floatBytes);
cudaMemcpy(testB0,d_B0,93185*sizeof(int),cudaMemcpyDeviceToHost);
float *testB1;
testB1 = (float *)malloc(floatBytes);
cudaMemcpy(testB1,d_B1,93185*sizeof(int),cudaMemcpyDeviceToHost);
float *testB2;
testB2 = (float *)malloc(floatBytes);
cudaMemcpy(testB2,d_B2,93185*sizeof(int),cudaMemcpyDeviceToHost);
float *testB3;
testB3 = (float *)malloc(floatBytes);
cudaMemcpy(testB3,d_B3,93185*sizeof(int),cudaMemcpyDeviceToHost);
for(int i=0;i<93185;i++){
if (A0[i] != testA0[i]) {
printf("Invalid kernel ");
break;}
}
free(A0);
free(testA0);
cudaFree(d_A0);
free(B0);
free(testB0);
cudaFree(d_B0);
free(B1);
free(testB1);
cudaFree(d_B1);
free(B2);
free(testB2);
cudaFree(d_B2);
free(B3);
free(testB3);
cudaFree(d_B3);
printf("\n");return 0; }