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

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

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

int main(int argc,char **argv) {
fprintf(stderr, "946 ");
int size=958696;
int intBytes = size*sizeof(int);
int floatBytes = size*sizeof(float);
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);
for(int i=0;i<958696;i++){
B0[i] = 17.0456734472+i+1;
B1[i] = 98.3686152211+i+1;
B2[i] = 35.909337662*i+1;
B3[i] = 11.4256399175+i+1;
B4[i] = 94.4641764195*i+1;
}
float *d_B0;
cudaMalloc((void **)&d_B0,958696*sizeof(float));
cudaMemcpy(d_B0,B0,958696*sizeof(float),cudaMemcpyHostToDevice);
float *d_B1;
cudaMalloc((void **)&d_B1,958696*sizeof(float));
cudaMemcpy(d_B1,B1,958696*sizeof(float),cudaMemcpyHostToDevice);
float *d_B2;
cudaMalloc((void **)&d_B2,958696*sizeof(float));
cudaMemcpy(d_B2,B2,958696*sizeof(float),cudaMemcpyHostToDevice);
float *d_B3;
cudaMalloc((void **)&d_B3,958696*sizeof(float));
cudaMemcpy(d_B3,B3,958696*sizeof(float),cudaMemcpyHostToDevice);
float *d_B4;
cudaMalloc((void **)&d_B4,958696*sizeof(float));
cudaMemcpy(d_B4,B4,958696*sizeof(float),cudaMemcpyHostToDevice);
int n = 3745*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_B0,d_B1,d_B2,d_B3,d_B4,958696);
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(B0,B1,B2,B3,B4,958696);
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);
float *testB0;
testB0 = (float *)malloc(floatBytes);
cudaMemcpy(testB0,d_B0,958696*sizeof(int),cudaMemcpyDeviceToHost);
float *testB1;
testB1 = (float *)malloc(floatBytes);
cudaMemcpy(testB1,d_B1,958696*sizeof(int),cudaMemcpyDeviceToHost);
float *testB2;
testB2 = (float *)malloc(floatBytes);
cudaMemcpy(testB2,d_B2,958696*sizeof(int),cudaMemcpyDeviceToHost);
float *testB3;
testB3 = (float *)malloc(floatBytes);
cudaMemcpy(testB3,d_B3,958696*sizeof(int),cudaMemcpyDeviceToHost);
float *testB4;
testB4 = (float *)malloc(floatBytes);
cudaMemcpy(testB4,d_B4,958696*sizeof(int),cudaMemcpyDeviceToHost);
for(int i=0;i<958696;i++){
}
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);
printf("\n");return 0; }