b’2021_Spring_Hw03_Part2.tar.gz’
#include
#include
#include
#include
void get_walltime(double* wcTime) {
struct timeval tp;
gettimeofday(&tp, NULL);
*wcTime = (double)(tp.tv_sec + tp.tv_usec/1000000.0);
}
// complex algorithm for evaluation
void matrix_mult_orig(double *A, double *B, double *C, int N)
{
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
{
C[i * N + j] = 0;
for (int k = 0; k < N; k++)
{
C[i * N + j] += A[i * N + k] * B[k * N + j];
}
}
}
void compareOutputs(double *output1, double *output2, int length)
{
for (int i = 0; i < length; i++)
for (int j = 0; j < length; j++)
{
if (output1[i * length + j] != output2[i * length + j])
{
printf("Outputs do not match! (%i, %i) (%f, %f)\n",
i, j,
output1[i * length + j], output2[i * length + j]);
return;
}
}
printf("Output match, test passed!\n");
}
int main(int argc, char *argv[])
{
int N = 2000;
double d_S, d_E;
// some declarations
double *A = new double[N * N];
double *B = new double[N * N];
double *C = new double[N * N];
double *orig_C = new double[N * N];
// populate memory with some random data
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
A[i * N + j] = i * i;
B[i * N + j] = (double)j / (double) (i + 1);
}
}
// run the original for functional verification
matrix_mult_orig(A, B, orig_C, N);
// start benchmark
get_walltime(&d_S);
// iterative test loop
matrix_mult(A, B, C, N);
// end benchmark
get_walltime(&d_E);
// check the two matrices
compareOutputs(orig_C, C, N);
// report results
printf("Elapsed time: %f\n", d_E - d_S);
// cleanup!
delete[] A;
delete[] B;
delete[] C;
delete[] orig_C;
return 0;
}
2021_Spring_Hw03_Part2/abc123_hw3_part2/hw3_part2.cpp
2021_Spring_Hw03_Part2/abc123_hw3_part2/hw3_part2.cpp
// complex algorithm for evaluation
void matrix_mult(double *A, double *B, double *C, int N)
{
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
{
C[i * N + j] = 0;
for (int k = 0; k < N; k++)
{
C[i * N + j] += A[i * N + k] * B[k * N + j];
}
}
}
CC = g++
CFLAGS = -I. -O3 -mavx
DEPS = hw3_part2.hpp
OBJ = hw3_part2.o
LIB = lib_hw3_part2.a
%.o: %.cpp $(DEPS)
$(CC) $(CFLAGS) -c -o $@ $<
$(LIB): $(OBJ)
ar rcs $@ $(OBJ)
.PHONY: clean
clean:
rm -f $(OBJ) $(LIB)
/**
* This is a simple header file that declares the function in the library.
*/
void matrix_mult(double *A, double *B, double *C, int N);
CC=g++
CFLAGS= -O3
DEPS =
OBJ = hw3_part2_test.o
EXEC = hw3_part2_test
LIB = _hw3_part2
LIB_DIR = abc123_hw3_part2
%.o: %.cpp $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS) -I. -I./$(LIB_DIR)
$(EXEC): $(OBJ)
module load gcc; \
cd $(LIB_DIR) && $(MAKE);
module load gcc; \
$(CC) -o $@ $^ $(CFLAGS) -L./$(LIB_DIR)/ -l$(LIB) -L.
.PHONY: clean
clean:
cd $(LIB_DIR) && $(MAKE) clean
rm -f $(OBJ) $(EXEC)
2021_Spring_Hw03_Part2/hw3_part2_test.cpp
2021_Spring_Hw03_Part2/abc123_hw3_part2/hw3_part2.cpp
2021_Spring_Hw03_Part2/abc123_hw3_part2/Makefile
2021_Spring_Hw03_Part2/abc123_hw3_part2/hw3_part2.hpp
2021_Spring_Hw03_Part2/Makefile