COMS 4995 Advanced Systems Programming

Index of 2024-9/code/03

Parent directory
Makefile
mult-ijk.c
mult-ikj.c
mult-kji.c
sum1.c
sum2.c

Makefile

CC=gcc
CFLAGS=-g -Wall -O0

.PHONY: default
default: sum1 sum2 mult-ijk mult-kji mult-ikj

sum1:

sum2:

mult-ijk:

mult-kji:

mult-ikj:

.PHONY: clean
clean:
	rm -f *.o sum1 sum2 mult-ijk mult-kji mult-ikj
	# remove debug symbols directory generated in macOS
	rm -rf *.dSYM

.PHONY: all
all: clean default

mult-ijk.c

#include <stdio.h>

#define N 2000
double a[N][N] = {{0}};
double b[N][N] = {{0}};
double c[N][N] = {{0}};

int main(int argc, char **argv) {
    double sum = 0;

    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            sum = 0;
            for (int k = 0; k < N; k++) {
                sum += a[i][k] * b[k][j];
            }
            c[i][j] += sum;
        }
    }
}

mult-ikj.c

#include <stdio.h>

#define N 2000
double a[N][N] = {{0}};
double b[N][N] = {{0}};
double c[N][N] = {{0}};

int main(int argc, char **argv) {
    double r = 0;

    for (int i = 0; i < N; i++) {
        for (int k = 0; k < N; k++) {
            r = a[i][k];
            for (int j = 0; j < N; j++) {
                c[i][j] += r * b[k][j];
            }
        }
    }
}

mult-kji.c

#include <stdio.h>

#define N 2000
double a[N][N] = {{0}};
double b[N][N] = {{0}};
double c[N][N] = {{0}};

int main(int argc, char **argv) {
    double r = 0;

    for (int k = 0; k < N; k++) {
        for (int j = 0; j < N; j++) {
            r = b[k][j];
            for (int i = 0; i < N; i++) {
                c[i][j] += a[i][k] * r;
            }
        }
    }
}

sum1.c

#include <stdio.h>

#define M 4096
#define N 4096
double a[M][N] = {{0}};

int main(int argc, char **argv) {
    double sum = 0;

    for (int i = 0; i < M; i++) {
        for (int j = 0; j < N; j++) {
            sum += a[i][j];
        }
    }

    printf("sum = %g\n", sum);
    return 0;
}

sum2.c

#include <stdio.h>

#define M 4096
#define N 4096
double a[M][N] = {{0}};

int main(int argc, char **argv) {
    double sum = 0;

    for (int j = 0; j < N; j++) {
        for (int i = 0; i < M; i++) {
            sum += a[i][j];
        }
    }

    printf("sum = %g\n", sum);
    return 0;
}