dataIo.cpp 3.59 KB
Newer Older
perdacherMartin's avatar
perdacherMartin committed
1 2
#include "dataIo.h"

Martin Perdacher's avatar
Martin Perdacher committed
3
void random_init_unif(double *array, const int N, const int D, const int INIT_SEED){
Martin Perdacher's avatar
Martin Perdacher committed
4 5
    #pragma omp parallel
    {
Martin Perdacher's avatar
Martin Perdacher committed
6 7
        const int ME = omp_get_thread_num();
        const int ALL_THREADS = omp_get_num_threads();
8
        VSLStreamStatePtr stream;
Martin Perdacher's avatar
Martin Perdacher committed
9
        const int SEED=(ME + 15) * INIT_SEED;
10 11
        const double LOWER_BOUND=0.0;
        const double UPPER_BOUND=1.0;
Martin Perdacher's avatar
Martin Perdacher committed
12

13
        int errcode = vslNewStream( &stream, VSL_BRNG_MCG31, SEED);
Martin Perdacher's avatar
Martin Perdacher committed
14

15 16 17
        if ( errcode != VSL_ERROR_OK && errcode != VSL_STATUS_OK ){
            printf("vslNewStream error. dataIo.cpp line 9.\n");
            exit(1);
Martin Perdacher's avatar
Martin Perdacher committed
18
        }
Martin Perdacher's avatar
Martin Perdacher committed
19

Martin Perdacher's avatar
Martin Perdacher committed
20 21 22 23 24 25 26 27 28
        const int imin = (N / ALL_THREADS) * ME;
        const int imax = (ME == ALL_THREADS - 1 ) ? N : (N / ALL_THREADS) * (ME+1);
        const int MY_N = imax - imin;

        errcode = vdRngUniform(VSL_RNG_METHOD_UNIFORM_STD, stream, MY_N * D, &array[imin*D], LOWER_BOUND, UPPER_BOUND);

        if ( errcode != VSL_ERROR_OK && errcode != VSL_STATUS_OK ){
            printf("vslNewStream error. dataIo.cpp line 24.\n");
            exit(1);
29
        }
Martin Perdacher's avatar
Martin Perdacher committed
30

perdacherMartin's avatar
perdacherMartin committed
31 32 33
    }
}

34
void read_file(double *array, const int N, const int D, char filename[], const bool IS_BINARY){
perdacherMartin's avatar
perdacherMartin committed
35 36 37 38 39
    FILE *fp;
    size_t counts = 0;
    size_t i=0,j=0;
    char line[MAX_LINE_LENGTH];
    char *token=NULL;
Martin Perdacher's avatar
Martin Perdacher committed
40
    const char space[2] = ",";
perdacherMartin's avatar
perdacherMartin committed
41 42 43 44 45 46 47 48

    fp = fopen(filename,"r");

    if ( fp == NULL ){
        fprintf(stderr, "File '%s' does not exists!", filename);
        exit(1);
    }

49
    if ( IS_BINARY ){
Martin Perdacher's avatar
Martin Perdacher committed
50
        // printf("isbinary!\n");
perdacherMartin's avatar
perdacherMartin committed
51
        // read binary file, everything at once
52
        counts = fread(array, N * D, sizeof(double), fp);
Martin Perdacher's avatar
Martin Perdacher committed
53
        // printf("%dx%d: %d readed\n", N, D, counts);
perdacherMartin's avatar
perdacherMartin committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
        if ( counts == 0 ) {
            fprintf(stderr, "Binary file '%s' could not be read. Wrong format.", filename);
            exit(1);
        }
    }else{
        // processing a text file
        // format: there are D double values each line. Each value is separated by a space character.
        // notice MAX_LINE_LENGTH = 2049
        i = 0;
        while ( fgets ( line, MAX_LINE_LENGTH, fp ) != NULL &&
                i < N ) {
            if ( line[0] != '%'){ // ignore '%' comment char
                token = strtok(line, space);
                j=0;

                while ( token != NULL &&
                        j < D ){
                    array[i*D + j] = atof(token); // 0.0 if no valid conversion
                    token = strtok(NULL, space);
                    j++;
                }
                i++;
            }
        }
    }

    fclose(fp);
}

void save_binary_file(double *array, const int N, const int D, char filename[]){
    FILE *fp=NULL;
    size_t counts = 0;

    fp = fopen(filename, "w");

    if ( fp == NULL ){
        fprintf(stderr, "Could not open file '%s'!", filename);
        exit(1);
    }

    counts = fwrite(array,sizeof(double) * N * D, 1, fp);

    if ( counts == 0 ){
        fprintf(stderr, "Error in writing file '%s'. Abort.", filename);
        exit(1);
    }

    fclose(fp);
}

void save_text_file(double *array, const int N, const int D, char filename[]){
    FILE *fp=NULL;
    size_t counts = 0;
    size_t i=0, j=0;
    char line[MAX_LINE_LENGTH];
    char strDouble[50];

    fp = fopen(filename, "w");

    if ( fp == NULL ){
        fprintf(stderr, "Could not open file '%s'!", filename);
        exit(1);
    }

    for ( i=0 ; i < N ; i++ ){
        strcpy(line, "");
        for ( j=0 ; j < D ; j++ ){
            strcpy(strDouble, "");
            sprintf(strDouble, "%f ", array[i*D + j]);
            strcat(line, strDouble);

        }
        fprintf(fp, "%s\n", line);

    }

    fclose(fp);
}