Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
.POSIX:
.SUFFIXES:

# Use the system's default C compiler
# was gnu89 reallyyyy necessary ....
# surely gradescope machines have a compiler which can do post Y2K C ....

CC = cc
CFLAGS = -Wall -Wpointer-arith -Wstrict-prototypes -std=c11 -fPIC

# Pathname of the pkg-config compatible utility
# (not using this for this assignment at all)
#PC = pkg-config

# Instructions to create an LSP db -- default is clangd with compile_flags.txt
# e.g. To use bear you can set DB="bear -- make" which will create compile_commands.json
DB = echo $(LDFLAGS) $(CFLAGS) | tr ' ' '\n' > compile_flags.txt

# Project Files
BIN=test-mergesort
OBJ=test-mergesort.o mergesort.o
SRC=$(OBJ:%.o=%.c)
DEP=$(OBJ:%.o=%.d)

all: $(BIN)

# Build binary from objects
$(BIN): $(OBJ)
$(CC) $(CFLAGS) -o $@ $(OBJ) $(LDFLAGS)

# Suffix rules to create .o and .d files from sources
.SUFFIXES: .c .o
.c.o:
$(CC) $(CFLAGS) $(INC) -c $<

.SUFFIXES: .c .d
.c.d:
$(CC) -MM $< -o $@

# Unit tests
test:
make -f merge_test.make

# Generate LSP database on each clean
db:
$(DB)

clean: db
rm -fr $(BIN)
rm -fr $(OBJ)
rm -fr $(DEP)

make -f merge_test.make clean

# For FreeBSD make use -include
-include $(DEP)
90 changes: 90 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
README.template

## Project Number/Title

* Authors: Izak Baldacchino (a1830164), Bunsarak Ann (a1827385), Matthew Edmonds-Wilson(a1850372)
* Group name: Assignment 3 Groups 190

## Overview

This program benchmarks the performance of the parallel merge sort algorithm.
Given an array of random integers of size N, and a "level", it performs merge
sort; each time the array is partitioned, a new thread is spawned to perform
the recursive merge sort on the sub-array. This continues until the "level"
is exceeded.

## Manifest

Makefile -- the recipes which are used to compile the project.
README.md -- this file!
mergesort.c -- contains the source for the functions which need to be implemented.
mergesort.h -- contains the declarations for the functions and structs used.
test-mergesort.c -- contains the source for the program which runs the algorithm.
merge_test.make -- tiny makefile to build the merge unit test

## Building the project

To build the project simply execute `make`. This assumes it is being built on a UNIX-like
system which features some rendition of the `make` utility. In fact, any system which is
POSIX-compliant shall be able to execute and run this makefile.

From this point execute the built executable `./test-mergesort` and provide relevant arguments.

macOS note: If you see a build error about <error.h>, we’ve already removed that include for macOS compatibility in this branch. If you pull a branch that still includes it, delete the line in test-mergesort.c.

## Features and usage

To run the program use `./test-mergesort N level seed` where `N` refers to the size of the generated
array, `level` refers to the number of times the array is partitioned and sorted in parallel, and
`seed` is used as the seed for generating the random array; same seed = same array.

TODO: write more perhaps?

## Testing
Unit tests for merge()
Balanced halves, unbalanced halves, left/right remainder cases, duplicates, already sorted, reverse-sorted, tiny segments (size 0/1).

Integration check (temporary)
While parallel_mergesort is incomplete, we verify my_mergesort correctness by temporarily routing parallel_mergesort to call my_mergesort (serial path) and checking the harness output. (In this branch, parallel_mergesort is intentionally a stub.)

This section should detail how you tested your code. Simply stating "I ran
it a few times and it seems to work" is not sufficient. Your testing needs
to be detailed here.


TODO: figure out testing scheme.

## Known Bugs

List known bugs that you weren't able to fix (or ran out of time to fix).

TODO: haven't made any bugs yet ...

## Reflection and Self Assessment
Early failure we hit: Harness printed “sorting failed!!!!” because it always calls parallel_mergesort() and ours was a stub.
What we learned: keep a unit test for merge() separate from threading so we can validate core correctness early.

Discuss the issues you encountered during development and testing. What
problems did you have? What did you have to research and learn on your own?
What kinds of errors did you get? How did you fix them?

What parts of the project did you find challenging? Is there anything that
finally "clicked" for you in the process of working on this project? How well
did the development and testing process go for you?


TODO: haven't finished yet ...

## Sources Used
Operating Systems: Three Easy Pieces — chapters on concurrency for general pthread patterns.
Assignment 3 specification and starter code (University of Adelaide, COMP SCI 3004–7064).
Assistance with structuring tests and clarifying merge-edge cases provided by AI (ChatGPT).
https://www.youtube.com/watch?v=b1n9T-Iy3SA
https://stackoverflow.com/questions/2550774/what-is-size-t-in-c

If you used any sources outside of the textbook, you should list them here.
If you looked something up on stackoverflow.com or you use help from AI, and
fail to cite it in this section, it will be considered plagiarism and dealt
with accordingly. So be safe CITE!

TODO: haven't used any sources yet ...
12 changes: 0 additions & 12 deletions comp2002-os-mergesort/Makefile

This file was deleted.

57 changes: 0 additions & 57 deletions comp2002-os-mergesort/README.template

This file was deleted.

27 changes: 0 additions & 27 deletions comp2002-os-mergesort/mergesort.c

This file was deleted.

95 changes: 95 additions & 0 deletions merge_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

#include "mergesort.h"

// global state is safe and good and never goes wrong and doesn't make testing difficult!
int* A;
int* B;

void printA(size_t start, size_t end) {
printf("\nA ");
for(size_t i = start; i < end+1; i++) {
printf("%d ", A[i]);
}
}

void printB(size_t start, size_t end) {
printf("\nB ");
for(size_t i = start; i < end+1; i++) {
printf("%d ", B[i]);
}
}

/* unit test for merge function */
int main(void) {
/* contrary to popular practice, malloc should not be casted
as void* is casted safely automatically */

/* first test: merge two halves of the full array */
{
printf("Test 1: merge two havles of full array\n");
A = malloc(10 * sizeof(int));
B = malloc(10 * sizeof(int));

A[0] = 0; /* leftstart = 0*/
A[1] = 1;
A[2] = 2;
A[3] = 3;
A[4] = 4; /* leftend = 4*/

A[5] = 3; /* rightstart = 5 */
A[6] = 4;
A[7] = 5;
A[8] = 6;
A[9] = 7; /* rightend = 9 */

/* expected result is A=[0,1,2,3,3,4,4,5,6,7] */
merge(0,4,5,9);

assert(A[0] == 0);
assert(A[1] == 1);
assert(A[2] == 2);
assert(A[3] == 3);
assert(A[4] == 3);
assert(A[5] == 4);
assert(A[6] == 4);
assert(A[7] == 5);
assert(A[8] == 6);
assert(A[9] == 7);

printf("Test 1: Passed\n");
}


/* first test: merge two halves of the full array */
{
printf("Test 2: merge two havles of sub-array\n");
A = malloc(10 * sizeof(int));
B = malloc(10 * sizeof(int));

A[0] = 5;
A[1] = 6;
A[2] = 7;
A[3] = 8;
A[4] = 9;

A[5] = 10; /* leftstart = 5 */
A[6] = 11; /* leftend = 6 */
A[7] = 2; /* rightstart = 7 */
A[8] = 3;
A[9] = 4; /* rightend = 9 */

/* expected result is A=[2,3,4,10,11] */
merge(5,6,7,9);

assert(A[5] == 2);
assert(A[6] == 3);
assert(A[7] == 4);
assert(A[8] == 10);
assert(A[9] == 11);

printf("Test 2: Passed\n");
}
}
50 changes: 50 additions & 0 deletions merge_test.make
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
.POSIX:
.SUFFIXES:

# Use the system's default C compiler
# was gnu89 reallyyyy necessary ....
# surely gradescope machines have a compiler which can do post Y2K C ....

CC = cc
CFLAGS = -Wall -Wpointer-arith -Wstrict-prototypes -std=c11 -fPIC

# Pathname of the pkg-config compatible utility
# (not using this for this assignment at all)
#PC = pkg-config

# Instructions to create an LSP db -- default is clangd with compile_flags.txt
# e.g. To use bear you can set DB="bear -- make" which will create compile_commands.json
DB = echo $(LDFLAGS) $(CFLAGS) | tr ' ' '\n' > compile_flags.txt

# Project Files
BIN=merge_test.test
OBJ=merge_test.o mergesort.o
SRC=$(OBJ:%.o=%.c)
DEP=$(OBJ:%.o=%.d)

all: $(BIN)

# Build binary from objects
$(BIN): $(OBJ)
$(CC) $(CFLAGS) -o $@ $(OBJ) $(LDFLAGS)

# Suffix rules to create .o and .d files from sources
.SUFFIXES: .c .o
.c.o:
$(CC) $(CFLAGS) $(INC) -c $<

.SUFFIXES: .c .d
.c.d:
$(CC) -MM $< -o $@

# Generate LSP database on each clean
db:
$(DB)

clean: db
rm -fr $(BIN)
rm -fr $(OBJ)
rm -fr $(DEP)

# For FreeBSD make use -include
-include $(DEP)
Loading