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

__global__ void kernel_gpu(int *A0,int *A1,int *A2,int *A3,float *B0,float *B1,float *B2,float *B3,float *B4,float *B5,float *B6,float *B7,int N){
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < N){
A0[i] = A1[i];
B0[i] = B5[i]+B7[i];
B1[i] = B5[i];
B2[i] = B7[i]-B6[i]*B7[i];
B3[i] = B7[i];
B4[i] = B5[i];
}
}

void kernel_cpu(int *A0,int *A1,int *A2,int *A3,float *B0,float *B1,float *B2,float *B3,float *B4,float *B5,float *B6,float *B7,int N){
for(int i=0;i<N;i++){
A0[i] = A1[i];
B0[i] = B5[i]+B7[i];
B1[i] = B5[i];
B2[i] = B7[i]-B6[i]*B7[i];
B3[i] = B7[i];
B4[i] = B5[i];
}
}

int main(int argc,char **argv) {
fprintf(stderr, "456 ");
int size=846897;
int intBytes = size*sizeof(int);
int floatBytes = size*sizeof(float);
int *A0;
A0 = (int *)malloc(intBytes);
int *A1;
A1 = (int *)malloc(intBytes);
int *A2;
A2 = (int *)malloc(intBytes);
int *A3;
A3 = (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);
float *B4;
B4 = (float *)malloc(floatBytes);
float *B5;
B5 = (float *)malloc(floatBytes);
float *B6;
B6 = (float *)malloc(floatBytes);
float *B7;
B7 = (float *)malloc(floatBytes);
for(int i=0;i<846897;i++){
A0[i] = 43+i+1;
A1[i] = 7*i+1;
A2[i] = 11+i+1;
A3[i] = 67+i+1;
B0[i] = 43.476880727+i+1;
B1[i] = 31.5099918519+i+1;
B2[i] = 93.1146635889*i+1;
B3[i] = 35.2893737828*i+1;
B4[i] = 72.3433785296*i+1;
B5[i] = 39.6976757695*i+1;
B6[i] = 28.1428145532+i+1;
B7[i] = 11.8799656429+i+1;
}
int *d_A0;
cudaMalloc((void **)&d_A0,846897*sizeof(int));
cudaMemcpy(d_A0,A0,846897*sizeof(int),cudaMemcpyHostToDevice);
int *d_A1;
cudaMalloc((void **)&d_A1,846897*sizeof(int));
cudaMemcpy(d_A1,A1,846897*sizeof(int),cudaMemcpyHostToDevice);
int *d_A2;
cudaMalloc((void **)&d_A2,846897*sizeof(int));
cudaMemcpy(d_A2,A2,846897*sizeof(int),cudaMemcpyHostToDevice);
int *d_A3;
cudaMalloc((void **)&d_A3,846897*sizeof(int));
cudaMemcpy(d_A3,A3,846897*sizeof(int),cudaMemcpyHostToDevice);
float *d_B0;
cudaMalloc((void **)&d_B0,846897*sizeof(float));
cudaMemcpy(d_B0,B0,846897*sizeof(float),cudaMemcpyHostToDevice);
float *d_B1;
cudaMalloc((void **)&d_B1,846897*sizeof(float));
cudaMemcpy(d_B1,B1,846897*sizeof(float),cudaMemcpyHostToDevice);
float *d_B2;
cudaMalloc((void **)&d_B2,846897*sizeof(float));
cudaMemcpy(d_B2,B2,846897*sizeof(float),cudaMemcpyHostToDevice);
float *d_B3;
cudaMalloc((void **)&d_B3,846897*sizeof(float));
cudaMemcpy(d_B3,B3,846897*sizeof(float),cudaMemcpyHostToDevice);
float *d_B4;
cudaMalloc((void **)&d_B4,846897*sizeof(float));
cudaMemcpy(d_B4,B4,846897*sizeof(float),cudaMemcpyHostToDevice);
float *d_B5;
cudaMalloc((void **)&d_B5,846897*sizeof(float));
cudaMemcpy(d_B5,B5,846897*sizeof(float),cudaMemcpyHostToDevice);
float *d_B6;
cudaMalloc((void **)&d_B6,846897*sizeof(float));
cudaMemcpy(d_B6,B6,846897*sizeof(float),cudaMemcpyHostToDevice);
float *d_B7;
cudaMalloc((void **)&d_B7,846897*sizeof(float));
cudaMemcpy(d_B7,B7,846897*sizeof(float),cudaMemcpyHostToDevice);
int n = 3309*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_A1,d_A2,d_A3,d_B0,d_B1,d_B2,d_B3,d_B4,d_B5,d_B6,d_B7,846897);
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,A1,A2,A3,B0,B1,B2,B3,B4,B5,B6,B7,846897);
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,846897*sizeof(int),cudaMemcpyDeviceToHost);
int *testA1;
testA1 = (int *)malloc(intBytes);
cudaMemcpy(testA1,d_A1,846897*sizeof(int),cudaMemcpyDeviceToHost);
int *testA2;
testA2 = (int *)malloc(intBytes);
cudaMemcpy(testA2,d_A2,846897*sizeof(int),cudaMemcpyDeviceToHost);
int *testA3;
testA3 = (int *)malloc(intBytes);
cudaMemcpy(testA3,d_A3,846897*sizeof(int),cudaMemcpyDeviceToHost);
float *testB0;
testB0 = (float *)malloc(floatBytes);
cudaMemcpy(testB0,d_B0,846897*sizeof(int),cudaMemcpyDeviceToHost);
float *testB1;
testB1 = (float *)malloc(floatBytes);
cudaMemcpy(testB1,d_B1,846897*sizeof(int),cudaMemcpyDeviceToHost);
float *testB2;
testB2 = (float *)malloc(floatBytes);
cudaMemcpy(testB2,d_B2,846897*sizeof(int),cudaMemcpyDeviceToHost);
float *testB3;
testB3 = (float *)malloc(floatBytes);
cudaMemcpy(testB3,d_B3,846897*sizeof(int),cudaMemcpyDeviceToHost);
float *testB4;
testB4 = (float *)malloc(floatBytes);
cudaMemcpy(testB4,d_B4,846897*sizeof(int),cudaMemcpyDeviceToHost);
float *testB5;
testB5 = (float *)malloc(floatBytes);
cudaMemcpy(testB5,d_B5,846897*sizeof(int),cudaMemcpyDeviceToHost);
float *testB6;
testB6 = (float *)malloc(floatBytes);
cudaMemcpy(testB6,d_B6,846897*sizeof(int),cudaMemcpyDeviceToHost);
float *testB7;
testB7 = (float *)malloc(floatBytes);
cudaMemcpy(testB7,d_B7,846897*sizeof(int),cudaMemcpyDeviceToHost);
for(int i=0;i<846897;i++){
if (A0[i] != testA0[i]) {
printf("Invalid kernel ");
break;}
if (A1[i] != testA1[i]) {
printf("Invalid kernel ");
break;}
if (A2[i] != testA2[i]) {
printf("Invalid kernel ");
break;}
if (A3[i] != testA3[i]) {
printf("Invalid kernel ");
break;}
}
free(A0);
free(testA0);
cudaFree(d_A0);
free(A1);
free(testA1);
cudaFree(d_A1);
free(A2);
free(testA2);
cudaFree(d_A2);
free(A3);
free(testA3);
cudaFree(d_A3);
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);
free(B4);
free(testB4);
cudaFree(d_B4);
free(B5);
free(testB5);
cudaFree(d_B5);
free(B6);
free(testB6);
cudaFree(d_B6);
free(B7);
free(testB7);
cudaFree(d_B7);
printf("\n");return 0; }