Skip to content

Commit

Permalink
Add CSC format code
Browse files Browse the repository at this point in the history
  • Loading branch information
brucehow committed Sep 10, 2019
1 parent 0506ad1 commit 389e269
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 8 deletions.
94 changes: 94 additions & 0 deletions format.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,98 @@ struct CSR csr_format(int rows, int cols, enum mat_type type, char *data) {
matrix.ia[i+1] = matrix.count;
}
return matrix;
}

struct CSC csc_format(int rows, int cols, enum mat_type type, char *data) {
// Structure variable initialisation
struct CSC matrix;
size_t nnz_size;
size_t ja_size = MEMSIZ * sizeof(int);
if (type == INT_MAT) {
nnz_size = MEMSIZ * sizeof(int);
matrix.nnz.i = allocate(nnz_size);
} else {
nnz_size = MEMSIZ * sizeof(float);
matrix.nnz.f = allocate(nnz_size);
}
matrix.ja = allocate(ja_size);
matrix.ia = allocate(sizeof(int) * (rows+1));
matrix.ia[0] = 0; // Conventional
matrix.cols = cols;
matrix.count = 0;

// Data reading variables
int len, pos = 0;
int skip = 0; // Use to read data column down
size_t size = MEMSIZ;
char *val = allocate(size);

for (int i = 0; i < cols; i++) {
// Figure out where to start col wise
skip = 0;
pos = 0;
while (skip != i) {
if (data[pos] == ' ') {
skip++;
}
pos++;
}
skip = 0;
for (int j = 0; j < rows; j++) {
len = 0; // Reset word length to 0

while (data[pos] != '\0' && data[pos] != ' ') {
val[len++] = data[pos++];

// Dynamic memory reallocation for each digit
if ((len * sizeof(char)) == size) {
if (data[pos] != '\0' && data[pos] != ' ') {
size += sizeof(char); // Null byte
} else {
size *= 2;
}
val = reallocate(val, size);
}
}
val[len] = '\0';
while (data[pos] != '\0' && skip != cols) { // Iterate column
if (data[pos] == ' ') {
skip++;
}
pos++;
}
skip = 0;
check_numeric(val);

// Zero value filter
if (type == INT_MAT) {
int value = atoi(val);
if (value != 0) {
// Dynamically allocate memory for nnz and ja pointers
if ((matrix.count * sizeof(int)) == ja_size) {
nnz_size *= 2;
ja_size *= 2;
matrix.nnz.i = reallocate(matrix.nnz.i, nnz_size);
matrix.ja = reallocate(matrix.ja, ja_size);
}
matrix.nnz.i[matrix.count] = value;
matrix.ja[matrix.count++] = j;
}
} else {
float value = atof(val);
if (value != 0.0) {
if ((matrix.count * sizeof(int)) == ja_size) {
nnz_size *= 2;
ja_size *= 2;
matrix.nnz.f = reallocate(matrix.nnz.f, nnz_size);
matrix.ja = reallocate(matrix.ja, ja_size);
}
matrix.nnz.f[matrix.count] = value;
matrix.ja[matrix.count++] = j;
}
}
}
matrix.ia[i+1] = matrix.count;
}
return matrix;
}
4 changes: 2 additions & 2 deletions matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ int main(int argc, char *argv[]) {
data = read_line(fp);

// Matrix processing
struct CSR matrix = csr_format(rows, cols, type, data);
struct CSC matrix = csc_format(rows, cols, type, data);

/////////////// DEBUG //////////////////
int limit = matrix.count;
Expand All @@ -29,7 +29,7 @@ int main(int argc, char *argv[]) {
printf("%d,", matrix.nnz.i[i]);
}
printf("\nia=");
for (int i = 0; i < limit+1; i++) {
for (int i = 0; i < cols+1; i++) {
printf("%d,", matrix.ia[i]);
}
printf("\nja=");
Expand Down
26 changes: 20 additions & 6 deletions matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

#define MEMSIZ 8


// Matrix type classifier
enum mat_type {INT_MAT, FLOAT_MAT};

Expand Down Expand Up @@ -48,6 +47,8 @@ extern struct COO coo_format(int rows, int cols, enum mat_type type, char *data)

extern struct CSR csr_format(int rows, int cols, enum mat_type type, char *data);

extern struct CSC csc_format(int rows, int cols, enum mat_type type, char *data);

/**
* Allocates memory of a given size using malloc
*
Expand All @@ -66,7 +67,7 @@ void *allocate(size_t size);
void *reallocate(void *ptr, size_t size);


// COO representation structs
// COO representation
struct COO {
enum mat_type type;
int count;
Expand All @@ -80,15 +81,28 @@ struct COO {
} *elements;
};

// CSR representation structs
// CSR representation
struct CSR {
enum mat_type type;
int rows;
int count; // Number of nrz values
int rows; // Used for IA length + 1
int count; // Number of nnz values
union {
int *i;
float *f;
} nnz; // List of non-zero values
int *ia; // Total number of elements up until specific row
int *ja; // List of column index for each nrz value
int *ja; // List of column index for each nnz value
};

// CSC representation
struct CSC {
enum mat_type type;
int cols; // Used for IA length + 1
int count; // Number of nnz values
union {
int *i;
float *f;
} nnz; // List of non-zero values
int *ia; // Total number of elements up until specific col
int *ja; // List of row index for each nnz value
};

0 comments on commit 389e269

Please sign in to comment.