summaryrefslogtreecommitdiff
path: root/SpMV/data/generate/replicate.c
blob: 4e06e83bc1a9c99c2265838d70bb5d25b1e527a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

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

typedef unsigned int uint32_t;

struct COOMatrix {
    uint32_t numRows;
    uint32_t numCols;
    uint32_t numNonzeros;
    uint32_t* rowIdxs;
    uint32_t* colIdxs;
};

static struct COOMatrix readCOOMatrix(const char* fileName) {

    struct COOMatrix cooMatrix;

    // Initialize fields
    FILE* fp = fopen(fileName, "r");
    assert(fscanf(fp, "%u", &cooMatrix.numRows));
    assert(fscanf(fp, "%u", &cooMatrix.numCols));
    assert(fscanf(fp, "%u", &cooMatrix.numNonzeros));
    cooMatrix.rowIdxs = (uint32_t*) malloc(cooMatrix.numNonzeros*sizeof(uint32_t));
    cooMatrix.colIdxs = (uint32_t*) malloc(cooMatrix.numNonzeros*sizeof(uint32_t));

    // Read the nonzeros
    for(uint32_t i = 0; i < cooMatrix.numNonzeros; ++i) {
        uint32_t rowIdx;
        assert(fscanf(fp, "%u", &rowIdx));
        cooMatrix.rowIdxs[i] = rowIdx - 1; // File format indexes begin at 1
        uint32_t colIdx;
        assert(fscanf(fp, "%u", &colIdx));
        cooMatrix.colIdxs[i] = colIdx - 1; // File format indexes begin at 1
    }

    fclose(fp);

    return cooMatrix;

}

static void freeCOOMatrix(struct COOMatrix cooMatrix) {
    free(cooMatrix.rowIdxs);
    free(cooMatrix.colIdxs);
}

int main(int argc, char** argv) {

    const char* fileName = (argc > 1)?argv[1]:"bcsstk30.mtx";
    unsigned int replicationFactor = (argc > 2)?atoi(argv[2]):4;
    const char* outFileName = (argc > 3)?argv[3]:"out.mtx";

    struct COOMatrix cooMatrix = readCOOMatrix(fileName);

    FILE* fp = fopen(outFileName, "w");
    fprintf(fp, "%u  %u  %u\n", cooMatrix.numRows*replicationFactor, cooMatrix.numCols, cooMatrix.numNonzeros*replicationFactor);
    for(unsigned int i = 0; i < cooMatrix.numNonzeros; ++i) {
        unsigned int row = cooMatrix.rowIdxs[i];
        unsigned int col = cooMatrix.colIdxs[i];
        for(unsigned int r = 0; r < replicationFactor; ++r) {
            fprintf(fp, "%u %u\n", row + 1 + r*cooMatrix.numRows, col + 1);
        }
    }
    fclose(fp);

    freeCOOMatrix(cooMatrix);

    return 0;

}