Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Inconsistent final print out in C code + Maybe byte or addressing issue but can't nail it down

Running a C code with an extension for CUDA to parallelize against the GPU. I am fairly certain this issue is not due to the CUDA code. I believe it has something to do with the reading in of the StockNames.txt file because I was producing the same results every time before adding that in. The final result I am speaking of is at the end of the code that prints out the TOTAL PROFITS. It should print out $226.48 and it does about half of the time. Other times it runs an has odd output like $913818171.00 or a few times I saw it print out $226.50. Nothing in the code changed between these results. I'm new to C so would love to understand the reasoning behind what is going on and how to fix it.The code is as follows:

#include <stdio.h>
#include <stdlib.h>
#include <cuda.h>


#define data 5522
#define dates 251
#define companies 22

//kernel function
__global__ void test(float *stockPick_d, float *results_d){

    int idx = threadIdx.x;
    for(idx=0;  idx < 252; idx++) {

        if(idx < 247) {
            if(stockPick_d[idx+1] < stockPick_d[idx]) {
                if(stockPick_d[idx+2] < stockPick_d[idx+1]) {
                    if(stockPick_d[idx+3] < stockPick_d[idx+2]) {
                        if(stockPick_d[idx+4] < stockPick_d[idx+3]) {
                            if(stockPick_d[idx+5] < stockPick_d[idx+4]) {
                                //number 1 add plus 5

                                results_d[idx+5] = 1; //  =1 means 5 days low.. we should BUY and hold for 10 day


                            }


                        }
                    }
                }


            }


        }

    }
}


int main()  {





    float database[data]; // [tradingdays] ;
    FILE *fp;
    int i,j;

    fp =fopen("InputStockData.txt","r");
    if(fp ==NULL) {
        printf("File did not open");
    }

    // (1)bring in data from .CSV file --> 2D of everything
    // (2)2D array with 1st column = company Index, and next columns are closing price per date
    for(i=0;i<data;i++) {
        if(i%251 == 0 ){
            //  printf("**DIVISIBLE BY 251: index = %d", i);
            //printf("\n");
        }

        //for(j=0;j<9;j++) {
        fscanf(fp, "%f\t", &database[i]);
        //  printf("%0.2f\t", database[i]);
    }

    fclose(fp);

    float data2D[companies][252];
    int k;

    // this converts the 1D array into a 2D array
    int count =0; // reinitialize i to 0.
    for(j=0;j<companies;j++){
        for(k=0;k < 251;k++){
            data2D[j][k]=database[count];
            count++;
        }
    }


    // device and host array initialization
    float stockPick_h[251];
    //float results_h[251] = {0};
    float *results_d;
    float *stockPick_d;

    // device allocation
    cudaMalloc( (void**) &results_d, dates*sizeof(float)); // change to 16384 when using 128x128
    cudaMalloc( (void**) &stockPick_d,  dates* sizeof(float));

    //final results array
    float finalResults[companies][252] = {{0}};
    int n,p;
    int counter;




    for(i=1; i < companies; i++) {
        //reallocating memory for kernel arrays for every [company] iteration
        cudaMalloc( (void**) &results_d, dates*sizeof(float)); 
        cudaMalloc( (void**) &stockPick_d,  dates* sizeof(float));
        float results_h[251] = {0};
        for(j=0; j < 251; j++) {
            stockPick_h[j] = data2D[i][j];

        }
        //copying memory from the CPU to the GPU
        cudaMemcpy(results_d, results_h, dates*sizeof(float), cudaMemcpyHostToDevice);
        cudaMemcpy(stockPick_d,stockPick_h, dates*sizeof(float), cudaMemcpyHostToDevice);
        //how many blocks we will assign
        dim3 DimGrid(1);
        //how many threads per block we will allocate
        dim3 DimBlock(251);
        //kernel launch for the GPU
        test<<<DimGrid, DimBlock>>>(stockPick_d, results_d);

        //copying the results back from the GPU to the CPU for printing
        cudaMemcpy(results_h, results_d, dates*sizeof(float), cudaMemcpyDeviceToHost);


        counter =0;
        for(p=0; p < 251; p++){
            //          printf("%.2f ", results_h[p]);
            finalResults[i][p] = results_h[counter];
            counter++;
        }

        //freeing up device arrays after every iteration
        cudaFree(stockPick_d);
        cudaFree(results_d);


    }
    printf("\n");
    //printf("***final results****\n\n");

    // printing out profit results for 5 day low instance
    float profit;
    float profits;
    float totalprofit[companies+1];


    printf("The stategy you have implemented is a 5-day low, 10-day hold!\n");

        printf("You have run a historical simulation for the Fiscal Year of 2017.\n");
        printf("The following companies are used in the sample:\n");
            char *stockNames[companies+1][8];
                //char buffer[7];
                FILE *file;
                file = fopen("StockNames.txt", "r");

                if(file == NULL) {
                    printf("File did not open\n");
                }

                for(i=0; i < companies; i++ ){
                    fscanf(file,"%s", &stockNames[i]);
                    printf("%s\n", stockNames[i]);
                }

                fclose(file);



        printf("The results will follow:\n\n");

    for(n=0; n< companies; n++){
        profits =0;
        if(n > 0) {
        for(p=0; p < 251; p++) {

            if(finalResults[n][p]== 1 && p <= 251-10) {

                profit = data2D[n][p+10] - data2D[n][p];


                printf("You bought Stock Ticker: %s because it hit a 5-day low\n", stockNames[n-1]);

                printf("You bought it at price = $%.2f held it for 10 days and sold it at price = $%.2f\n", data2D[n][p], data2D[n][p+10]);

                printf("Your profit for this transaction = $%.2f\n", profit);

                printf("**************************************************************\n");


                profits+= profit;
                totalprofit[n] = profits;

            }
        }


        printf("Total profit for Company: %s is = $%.2f\n", stockNames[n-1],totalprofit[n]);
        printf("\n");
        }
    }


    float simulationProfit=0;

    for(i=0;i<companies; i++){

        simulationProfit= simulationProfit+ totalprofit[i];

    }

    printf("\n\n");
    printf("*****TOTAL PROFITS FOR THE SIMULATION WERE = $%.2f*****", simulationProfit);






}

Comments