Skip to content

Commit 4c5df38

Browse files
committed
boilerplate code
2 parents abef6cd + b4a75cb commit 4c5df38

File tree

7 files changed

+289
-2
lines changed

7 files changed

+289
-2
lines changed

README.md

-2
This file was deleted.

lab2_io.c

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "lab2_io.h"
2+
3+
void read_matrix (const char* input_filename, int* M, int* N, float** D){
4+
FILE *fin = fopen(input_filename, "r");
5+
6+
fscanf(fin, "%d%d", M, N);
7+
8+
int num_elements = (*M) * (*N);
9+
*D = (float*) malloc(sizeof(float)*(num_elements));
10+
11+
for (int i = 0; i < num_elements; i++){
12+
fscanf(fin, "%f", (*D + i));
13+
}
14+
fclose(fin);
15+
}
16+
17+
void write_result (int M,
18+
int N,
19+
float* D,
20+
float* U,
21+
float* SIGMA,
22+
float* V_T,
23+
int K,
24+
float* D_HAT,
25+
double computation_time){
26+
// Will contain output code
27+
}

lab2_io.h

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#ifndef LAB2_IO_H
2+
#define LAB2_IO_H
3+
4+
#include <stdio.h>
5+
#include <malloc.h>
6+
7+
/*
8+
M : number of rows (samples) in input matrix D
9+
N : number of columns (features) in input matrix D
10+
D : 1D Array of M x N input matrix in row-major,
11+
#elements in D is (M * N)
12+
--------------------------------------------------------------------------------------
13+
| D[0][0] | D[0][1] | ... | D[0][N-1] | D[1][0] | ... | D[1][N-1] | ... | D[M-1][N-1] |
14+
--------------------------------------------------------------------------------------
15+
*/
16+
void read_matrix (const char* input_filename, int* M, int *N, float** D);
17+
18+
/*
19+
check correctess of Singular Vector Decomposition
20+
Arguments:
21+
M : number of rows (samples) in input matrix D
22+
N : number of columns (features) in input matrix D
23+
D : 1D Array of M x N input matrix in row-major,
24+
#elements in D is (M * N)
25+
--------------------------------------------------------------------------------------
26+
| D[0][0] | D[0][1] | ... | D[0][N-1] | D[1][0] | ... | D[1][N-1] | ... | D[M-1][N-1] |
27+
--------------------------------------------------------------------------------------
28+
U : 1D array of N x n real matrix (computed by SVD) in row-major
29+
--------------------------------------------------------------------------------------
30+
| U[0][0] | U[0][1] | ... | U[0][N-1] | U[1][0] | ... | U[1][N-1] | ... | U[N-1][N-1] |
31+
--------------------------------------------------------------------------------------
32+
SIGMA : 1D array of N x M diagonal matrix of positive real numbers (computed by SVD),
33+
consisting only digonal elements.
34+
#elements in SIGMA is N
35+
-------------------------------------------------------------------
36+
| SIGMA[0][0] | SIGMA[1][1] | SIGMA[2][2] | ... | SIGMA[N-1][N-1] |
37+
-------------------------------------------------------------------
38+
V_T : 1D array of M x M real matrix (computed by SVD) in row-major
39+
--------------------------------------------------------------------------------
40+
| V_T[0][0] | V_T[0][1] | ... | V_T[0][M-1] | V_T[1][0] | ... | V_T[M-1][M-1] |
41+
--------------------------------------------------------------------------------
42+
K : number of coulmns (features) in reduced matrix D_HAT
43+
D_HAT : reduced matrix (computed by PCA) in row-major
44+
-------------------------------------------------------------------------------------
45+
| D_HAT[0][0] | D_HAT[0][1] | ... | D_HAT[0][K-1] | D_HAT[1][0] | ... | D[M-1][K-1] |
46+
-------------------------------------------------------------------------------------
47+
computation_time : Time elapsed in computing SVD and PCA
48+
*/
49+
50+
void write_result (int M,
51+
int N,
52+
float* D,
53+
float* U,
54+
float* SIGMA,
55+
float* V_T,
56+
int K,
57+
float* D_HAT,
58+
double computation_time);
59+
60+
#endif

lab2_omp.c

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <malloc.h>
2+
#include <omp.h>
3+
4+
5+
// /*
6+
// *****************************************************
7+
// TODO -- You must implement this function
8+
// *****************************************************
9+
// */
10+
void SVD(int M, int N, float* D, float** U, float** SIGMA, float** V_T)
11+
{
12+
}
13+
14+
// /*
15+
// *****************************************************
16+
// TODO -- You must implement this function
17+
// *****************************************************
18+
// */
19+
void PCA(int retention, int M, int N, float* D, float* U, float* SIGMA, float** D_HAT, int *K)
20+
{
21+
22+
}

lab2_omp.h

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#ifndef LAB2_OMP_H
2+
#define LAB2_OMP_H
3+
4+
5+
/*
6+
To be implemented
7+
Arguments:
8+
M : number of rows (samples) in input matrix D (input)
9+
N : number of columns (features) in input matrix D (input)
10+
D : 1D Array of M x N input matrix in row-major, (input)
11+
#elements in D is (M * N)
12+
--------------------------------------------------------------------------------------
13+
| D[0][0] | D[0][1] | ... | D[0][N-1] | D[1][0] | ... | D[1][N-1] | ... | D[M-1][N-1] |
14+
--------------------------------------------------------------------------------------
15+
U : 1D array of N x N real matrix in row-major (to be computed)
16+
--------------------------------------------------------------------------------------
17+
| U[0][0] | U[0][1] | ... | U[0][N-1] | U[1][0] | ... | U[1][N-1] | ... | U[N-1][N-1] |
18+
--------------------------------------------------------------------------------------
19+
SIGMA : 1D array of N x M diagonal matrix of positive real numbers (to be computed)
20+
format: consists only digonal elements
21+
#elements in SIGMA is N
22+
-------------------------------------------------------------------
23+
| SIGMA[0][0] | SIGMA[1][1] | SIGMA[2][2] | ... | SIGMA[N-1][N-1] |
24+
-------------------------------------------------------------------
25+
V_T : 1D array of M x M real matrix in row-major (to be computed)
26+
-------------------------------------------------------------------------------
27+
| V_T[0][0] | V_T[0][1] | ... | V_T[0][M-1] | V_T[1][0] | ... | V_T[M-1][M-1] |
28+
-------------------------------------------------------------------------------
29+
*/
30+
void SVD(int M, int N, float* D, float** U, float** SIGMA, float** V_T);
31+
32+
/*
33+
To be implemented
34+
Arguments:
35+
retention : percentage of information to be retained by PCA, ie
36+
retention = 90 means 90% of information should be retained
37+
M : number of rows (samples) in input matrix D (input)
38+
N : number of columns (features) in input matrix D (input)
39+
D : 1D Array of M x N input matrix in row-major, (input)
40+
#elements in D is (M * N)
41+
--------------------------------------------------------------------------------------
42+
| D[0][0] | D[0][1] | ... | D[0][N-1] | D[1][0] | ... | D[1][N-1] | ... | D[M-1][N-1] |
43+
--------------------------------------------------------------------------------------
44+
U : 1D array of N x N real matrix in row-major (input)
45+
--------------------------------------------------------------------------------------
46+
| U[0][0] | U[0][1] | ... | U[0][N-1] | U[1][0] | ... | U[1][N-1] | ... | U[N-1][N-1] |
47+
--------------------------------------------------------------------------------------
48+
SIGMA : 1D array of N x M diagonal matrix of positive real numbers (input)
49+
format: consists only digonal elements
50+
#elements in SIGMA is N
51+
-------------------------------------------------------------------
52+
| SIGMA[0][0] | SIGMA[1][1] | SIGMA[2][2] | ... | SIGMA[N-1][N-1] |
53+
-------------------------------------------------------------------
54+
D_HAT : 1D array of reduced M x K real matrix in row-major (to be computed)
55+
-----------------------------------------------------------------------------------------
56+
| D_HAT[0][0] | D_HAT[0][1] | ... | D_HAT[0][K-1] | D_HAT[1][0] | ... | D_HAT[M-1][K-1] |
57+
-----------------------------------------------------------------------------------------
58+
K : number of columns (features) in reduced matrix (to be computed)
59+
*/
60+
void PCA(int retention, int M, int N, float* D, float* U, float* SIGMA, float** D_HAT, int *K);
61+
62+
#endif

main_omp.c

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include "lab2_io.h"
2+
#include "lab2_omp.h"
3+
4+
#include <stdlib.h>
5+
#include <omp.h>
6+
7+
/*
8+
Arguments:
9+
arg1: input filename (consist M, N and D)
10+
arg2: retention (percentage of information to be retained by PCA)
11+
*/
12+
13+
int main(int argc, char const *argv[])
14+
{
15+
if (argc < 3){
16+
printf("\nLess Arguments\n");
17+
return 0;
18+
}
19+
20+
if (argc > 3){
21+
printf("\nTOO many Arguments\n");
22+
return 0;
23+
}
24+
25+
//---------------------------------------------------------------------
26+
int M; //no of rows (samples) in input matrix D (input)
27+
int N; //no of columns (features) in input matrix D (input)
28+
float* D; //1D array of M x N matrix to be reduced (input)
29+
float* U; //1D array of N x N matrix U (to be computed by SVD)
30+
float* SIGMA; //1D array of N x M diagonal matrix SIGMA (to be computed by SVD)
31+
float* V_T; //1D array of M x M matrix V_T (to be computed by SVD)
32+
int K; //no of coulmns (features) in reduced matrix D_HAT (to be computed by PCA)
33+
float *D_HAT; //1D array of M x K reduced matrix (to be computed by PCA)
34+
int retention; //percentage of information to be retained by PCA (command line input)
35+
//---------------------------------------------------------------------
36+
37+
retention = atoi(argv[2]); //retention = 90 means 90% of information should be retained
38+
39+
float start_time, end_time;
40+
double computation_time;
41+
42+
/*
43+
-- Pre-defined function --
44+
reads matrix and its dimentions from input file and creats array D
45+
#elements in D is M * N
46+
format -
47+
--------------------------------------------------------------------------------------
48+
| D[0][0] | D[0][1] | ... | D[0][N-1] | D[1][0] | ... | D[1][N-1] | ... | D[M-1][N-1] |
49+
--------------------------------------------------------------------------------------
50+
*/
51+
read_matrix (argv[1], &M, &N, &D);
52+
53+
U = (float*) malloc(sizeof(float) * N*N);
54+
SIGMA = (float*) malloc(sizeof(float) * N);
55+
V_T = (float*) malloc(sizeof(float) * M*M);
56+
57+
start_time = omp_get_wtime();
58+
59+
// /*
60+
// *****************************************************
61+
// TODO -- You must implement these two function
62+
// *****************************************************
63+
// */
64+
SVD(M, N, D, &U, &SIGMA, &V_T);
65+
PCA(retention, M, N, D, U, SIGMA, &D_HAT, &K);
66+
67+
end_time = omp_get_wtime();
68+
computation_time = ((double) (end_time - start_time));
69+
70+
/*
71+
--Pre-defined functions --
72+
checks for correctness of results computed by SVD and PCA
73+
and outputs the results
74+
*/
75+
write_result(M, N, D, U, SIGMA, V_T, K, D_HAT, computation_time);
76+
77+
return 0;
78+
}

testcase/gen_testcase.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/python3
2+
3+
#########################################################################
4+
# Generate M x N matrix of real numbers and store #
5+
# the the matrix in file named 'testcase_<M>_<N>' #
6+
# Parameters: #
7+
# M :no of rows (samples) in matrix #
8+
# N :no of coulmns (features) in matrix #
9+
# lrange, urange :range of matrix elements ie #
10+
# forall 0<=i<M, 0<=j<N #
11+
# lrange <= matrix[i][j] <= urange #
12+
# Format of output file: #
13+
# ----------------------------------------------------------------- #
14+
# | M N #
15+
# | D[0][0] D[0][1] ... D[0][N-1] D[1][0] ... D[M-1][N-1] #
16+
# ----------------------------------------------------------------- #
17+
#########################################################################
18+
19+
20+
from random import uniform
21+
22+
M = 10000 # number of rows (samples) in input matrix D
23+
N = 300 # number of columns (features) in input matrix
24+
lrange = -100000 # lrange <= element of matrix
25+
urange = 100000 # element of matrix <= urange
26+
27+
# number of elements in M x N matrix
28+
num_elements = M * N
29+
30+
filename = 'testcase_' + str(M) + '_' + str(N) #output filename
31+
file = open(filename, 'w')
32+
33+
# write size of matrix in first line of file
34+
file.write(str(M) + ' ' +str(N) + '\n')
35+
36+
# write space separated matrix elements
37+
for i in range(num_elements):
38+
file.write(str(uniform(lrange, urange)) + ' ')
39+
40+
file.close()

0 commit comments

Comments
 (0)