├── Assignments ├── MIT6_172F18_hw1 │ ├── __MACOSX │ │ ├── ._c-primer │ │ ├── ._matrix-multiply │ │ ├── c-primer │ │ │ ├── ._Makefile │ │ │ ├── ._pointer.c │ │ │ ├── ._preprocess.c │ │ │ ├── ._sizes.c │ │ │ ├── ._swap.c │ │ │ └── ._verifier.py │ │ └── matrix-multiply │ │ │ ├── ._.DS_Store │ │ │ ├── ._Makefile │ │ │ ├── ._fasttime.h │ │ │ ├── ._matrix_multiply.c │ │ │ ├── ._matrix_multiply.h │ │ │ ├── ._tbassert.h │ │ │ └── ._testbed.c │ ├── c-primer │ │ ├── Makefile │ │ ├── pointer.c │ │ ├── preprocess.c │ │ ├── sizes.c │ │ ├── swap.c │ │ └── verifier.py │ └── matrix-multiply │ │ ├── .DS_Store │ │ ├── Makefile │ │ ├── fasttime.h │ │ ├── matrix_multiply.c │ │ ├── matrix_multiply.h │ │ ├── tbassert.h │ │ └── testbed.c ├── MIT6_172F18_hw2 │ ├── __MACOSX │ │ ├── ._homework │ │ ├── ._recitation │ │ ├── homework │ │ │ ├── ._Makefile │ │ │ ├── ._fasttime.h │ │ │ ├── ._isort.c │ │ │ ├── ._main.c │ │ │ ├── ._sort.h │ │ │ ├── ._sort_a.c │ │ │ ├── ._sort_c.c │ │ │ ├── ._sort_f.c │ │ │ ├── ._sort_i.c │ │ │ ├── ._sort_m.c │ │ │ ├── ._sort_p.c │ │ │ ├── ._tests.c │ │ │ ├── ._tests.h │ │ │ ├── ._util.c │ │ │ └── ._util.h │ │ └── recitation │ │ │ ├── ._Makefile │ │ │ ├── ._isort.c │ │ │ ├── ._qsort.c │ │ │ ├── ._sum.c │ │ │ └── ._verifier.py │ ├── homework │ │ ├── Makefile │ │ ├── fasttime.h │ │ ├── isort.c │ │ ├── main.c │ │ ├── sort.h │ │ ├── sort_a.c │ │ ├── sort_c.c │ │ ├── sort_f.c │ │ ├── sort_i.c │ │ ├── sort_m.c │ │ ├── sort_p.c │ │ ├── tests.c │ │ ├── tests.h │ │ ├── util.c │ │ └── util.h │ └── recitation │ │ ├── Makefile │ │ ├── isort.c │ │ ├── qsort.c │ │ ├── sum.c │ │ └── verifier.py ├── MIT6_172F18_hw3 │ ├── __MACOSX │ │ ├── ._homework3 │ │ ├── ._recitation3 │ │ ├── homework3 │ │ │ ├── ._.DS_Store │ │ │ ├── ._Makefile │ │ │ ├── ._fasttime.h │ │ │ └── ._loop.c │ │ └── recitation3 │ │ │ ├── ._.DS_Store │ │ │ ├── ._.cflags │ │ │ ├── ._Makefile │ │ │ ├── ._example1.c │ │ │ ├── ._example1.o │ │ │ ├── ._example2.c │ │ │ ├── ._example2.o │ │ │ ├── ._example3.c │ │ │ ├── ._example3.o │ │ │ ├── ._example4.c │ │ │ └── ._example4.o │ ├── homework3 │ │ ├── .DS_Store │ │ ├── Makefile │ │ ├── fasttime.h │ │ └── loop.c │ └── recitation3 │ │ ├── .DS_Store │ │ ├── .cflags │ │ ├── Makefile │ │ ├── example1.c │ │ ├── example1.o │ │ ├── example2.c │ │ ├── example2.o │ │ ├── example3.c │ │ ├── example3.o │ │ ├── example4.c │ │ └── example4.o ├── MIT6_172F18_hw7 │ ├── Makefile │ ├── __MACOSX │ │ ├── ._Makefile │ │ ├── ._fib_cilk.c │ │ ├── ._fib_serial.c │ │ ├── ._func-analysis.c │ │ ├── ._linreg.c │ │ └── ._null-tool.c │ ├── fib_cilk.c │ ├── fib_serial.c │ ├── func-analysis.c │ ├── linreg.c │ └── null-tool.c ├── MIT6_172F18_hw9 │ ├── Makefile │ ├── __MACOSX │ │ ├── ._Makefile │ │ ├── ._common.h │ │ ├── ._hashlock.c │ │ ├── ._hashlock.h │ │ ├── ._hashtable.c │ │ ├── ._undef.c │ │ └── ._user.c │ ├── common.h │ ├── hashlock.c │ ├── hashlock.h │ ├── hashtable.c │ ├── undef.c │ └── user.c ├── MIT6_172F18hw1.pdf ├── MIT6_172F18hw10.pdf ├── MIT6_172F18hw2.pdf ├── MIT6_172F18hw3.pdf ├── MIT6_172F18hw4.pdf ├── MIT6_172F18hw5.pdf ├── MIT6_172F18hw6.pdf ├── MIT6_172F18hw7.pdf ├── MIT6_172F18hw8.pdf ├── MIT6_172F18hw9.pdf └── clint.py ├── Lectures ├── MIT6_172F18_lec1.pdf ├── MIT6_172F18_lec10.pdf ├── MIT6_172F18_lec11.pdf ├── MIT6_172F18_lec12.pdf ├── MIT6_172F18_lec13.pdf ├── MIT6_172F18_lec14.pdf ├── MIT6_172F18_lec15.pdf ├── MIT6_172F18_lec16.pdf ├── MIT6_172F18_lec17.pdf ├── MIT6_172F18_lec18.pdf ├── MIT6_172F18_lec19.pdf ├── MIT6_172F18_lec2.pdf ├── MIT6_172F18_lec20.pdf ├── MIT6_172F18_lec21.pdf ├── MIT6_172F18_lec22.pdf ├── MIT6_172F18_lec23.pdf ├── MIT6_172F18_lec3.pdf ├── MIT6_172F18_lec4.pdf ├── MIT6_172F18_lec5.pdf ├── MIT6_172F18_lec6.pdf ├── MIT6_172F18_lec7.pdf ├── MIT6_172F18_lec8.pdf └── MIT6_172F18_lec9.pdf └── README.md /Assignments/MIT6_172F18_hw1/__MACOSX/._c-primer: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw1/__MACOSX/._c-primer -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw1/__MACOSX/._matrix-multiply: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw1/__MACOSX/._matrix-multiply -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw1/__MACOSX/c-primer/._Makefile: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw1/__MACOSX/c-primer/._Makefile -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw1/__MACOSX/c-primer/._pointer.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw1/__MACOSX/c-primer/._pointer.c -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw1/__MACOSX/c-primer/._preprocess.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw1/__MACOSX/c-primer/._preprocess.c -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw1/__MACOSX/c-primer/._sizes.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw1/__MACOSX/c-primer/._sizes.c -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw1/__MACOSX/c-primer/._swap.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw1/__MACOSX/c-primer/._swap.c -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw1/__MACOSX/c-primer/._verifier.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw1/__MACOSX/c-primer/._verifier.py -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw1/__MACOSX/matrix-multiply/._.DS_Store: -------------------------------------------------------------------------------- 1 | Mac OS X  2Fx @ATTRxx -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw1/__MACOSX/matrix-multiply/._Makefile: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw1/__MACOSX/matrix-multiply/._Makefile -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw1/__MACOSX/matrix-multiply/._fasttime.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw1/__MACOSX/matrix-multiply/._fasttime.h -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw1/__MACOSX/matrix-multiply/._matrix_multiply.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw1/__MACOSX/matrix-multiply/._matrix_multiply.c -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw1/__MACOSX/matrix-multiply/._matrix_multiply.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw1/__MACOSX/matrix-multiply/._matrix_multiply.h -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw1/__MACOSX/matrix-multiply/._tbassert.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw1/__MACOSX/matrix-multiply/._tbassert.h -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw1/__MACOSX/matrix-multiply/._testbed.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw1/__MACOSX/matrix-multiply/._testbed.c -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw1/c-primer/Makefile: -------------------------------------------------------------------------------- 1 | CC := clang 2 | 3 | ifeq ($(DEBUG),1) 4 | CFLAGS := -Wall -O0 -g 5 | else 6 | CFLAGS := -Wall -O1 -DNDEBUG 7 | endif 8 | 9 | LDFLAGS := -lrt -flto -fuse-ld=gold 10 | 11 | all: sizes pointer swap 12 | 13 | sizes.o: sizes.c 14 | $(CC) $(CFLAGS) -c sizes.c 15 | 16 | sizes: sizes.o 17 | $(CC) -o sizes sizes.o $(LDFLAGS) 18 | 19 | pointer.o: pointer.c 20 | $(CC) $(CFLAGS) -c pointer.c 21 | 22 | pointer: pointer.o 23 | $(CC) -o pointer pointer.o $(LDFLAGS) 24 | 25 | swap.o: swap.c 26 | $(CC) $(CFLAGS) -c swap.c 27 | 28 | swap: swap.o 29 | $(CC) -o swap swap.o $(LDFLAGS) 30 | clean: 31 | rm -f sizes pointer swap *.o *.gcda *.gcno *.gcov perf.data */perf.data cachegrind.out.* 32 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw1/c-primer/pointer.c: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012 MIT License by 6.172 Staff 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | int main(int argc, char * argv[]) { // What is the type of argv? 8 | int i = 5; 9 | // The & operator here gets the address of i and stores it into pi 10 | int * pi = &i; 11 | // The * operator here dereferences pi and stores the value -- 5 -- 12 | // into j. 13 | int j = *pi; 14 | 15 | char c[] = "6.172"; 16 | char * pc = c; // Valid assignment: c acts like a pointer to c[0] here. 17 | char d = *pc; 18 | printf("char d = %c\n", d); // What does this print? 19 | 20 | // compound types are read right to left in C. 21 | // pcp is a pointer to a pointer to a char, meaning that 22 | // pcp stores the address of a char pointer. 23 | char ** pcp; 24 | pcp = argv; // Why is this assignment valid? 25 | 26 | const char * pcc = c; // pcc is a pointer to char constant 27 | char const * pcc2 = c; // What is the type of pcc2? 28 | 29 | // For each of the following, why is the assignment: 30 | *pcc = '7'; // invalid? 31 | pcc = *pcp; // valid? 32 | pcc = argv[0]; // valid? 33 | 34 | char * const cp = c; // cp is a const pointer to char 35 | // For each of the following, why is the assignment: 36 | cp = *pcp; // invalid? 37 | cp = *argv; // invalid? 38 | *cp = '!'; // valid? 39 | 40 | const char * const cpc = c; // cpc is a const pointer to char const 41 | // For each of the following, why is the assignment: 42 | cpc = *pcp; // invalid? 43 | cpc = argv[0]; // invalid? 44 | *cpc = '@'; // invalid? 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw1/c-primer/preprocess.c: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012 MIT License by 6.172 Staff 2 | 3 | // All occurences of ONE will be replaced by 1. 4 | #define ONE 1 5 | 6 | // Macros can also behave similar to inline functions. 7 | // Note that parentheses around arguments are required to preserve order of 8 | // operations. Otherwise, you can introduce bugs when substitution happens 9 | 10 | #define MIN(a, b) ((a) < (b) ? (a) : (b)) 11 | 12 | int c = ONE, d = ONE + 5; 13 | int e = MIN(c, d); 14 | 15 | #ifndef NDEBUG 16 | // This code will be compiled only when 17 | // the macro NDEBUG is not defined. 18 | // Recall that if clang is passed -DNDEBUG on the command line, 19 | // then NDEBUG will be defined. 20 | if (something) {} 21 | #endif 22 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw1/c-primer/sizes.c: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012 MIT License by 6.172 Staff 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | int main() { 8 | // Please print the sizes of the following types: 9 | // int, short, long, char, float, double, unsigned int, long long 10 | // uint8_t, uint16_t, uint32_t, and uint64_t, uint_fast8_t, 11 | // uint_fast16_t, uintmax_t, intmax_t, __int128, and student 12 | 13 | // Here's how to show the size of one type. See if you can define a macro 14 | // to avoid copy pasting this code. 15 | printf("size of %s : %zu bytes \n", "int", sizeof(int)); 16 | // e.g. PRINT_SIZE("int", int); 17 | // PRINT_SIZE("short", short); 18 | 19 | // Alternatively, you can use stringification 20 | // (https://gcc.gnu.org/onlinedocs/cpp/Stringification.html) so that 21 | // you can write 22 | // e.g. PRINT_SIZE(int); 23 | // PRINT_SIZE(short); 24 | 25 | // Composite types have sizes too. 26 | typedef struct { 27 | int id; 28 | int year; 29 | } student; 30 | 31 | student you; 32 | you.id = 12345; 33 | you.year = 4; 34 | 35 | 36 | // Array declaration. Use your macro to print the size of this. 37 | int x[5]; 38 | 39 | // You can just use your macro here instead: PRINT_SIZE("student", you); 40 | printf("size of %s : %zu bytes \n", "student", sizeof(you)); 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw1/c-primer/swap.c: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012 MIT License by 6.172 Staff 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | void swap(int i, int j) { 8 | int temp = i; 9 | i = j; 10 | j = temp; 11 | } 12 | 13 | int main() { 14 | int k = 1; 15 | int m = 2; 16 | swap(k, m); 17 | // What does this print? 18 | printf("k = %d, m = %d\n", k, m); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw1/c-primer/verifier.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | import subprocess, os, sys, re 3 | 4 | def exitWithError(error): 5 | print error 6 | sys.exit(1) 7 | 8 | def runAndReadOutput(args): 9 | if args is str: 10 | args = [args] 11 | try: 12 | return subprocess.check_output(args) 13 | except subprocess.CalledProcessError as e: 14 | exitWithError("ERROR: runtime error with %s" % str(args)) 15 | 16 | def run(path): runAndReadOutput(path) 17 | 18 | def runAndCheckSizes(): 19 | output = runAndReadOutput("./sizes") 20 | expected_output_format = "size of %s : %d bytes" 21 | 22 | lines = set([x.replace(" ", "") for x in output.strip().lower().split('\n')]) 23 | types = [ 24 | ( "int", 4 ), 25 | ( "short", 2 ), 26 | ( "long", 8 ), 27 | ( "char", 1 ), 28 | ( "float", 4 ), 29 | ( "double", 8 ), 30 | ( "unsigned int", 4 ), 31 | ( "long long", 8 ), 32 | ( "uint8_t", 1 ), 33 | ( "uint16_t", 2 ), 34 | ( "uint32_t", 4 ), 35 | ( "uint64_t", 8 ), 36 | ( "uint_fast8_t", 1 ), 37 | ( "uint_fast16_t", 8 ), 38 | ( "uintmax_t", 8 ), 39 | ( "intmax_t", 8 ), 40 | ( "__int128", 16 ), 41 | ( "uint32_t", 4 ), 42 | ( "uint64_t", 8 ), 43 | ( "student", 8 ), 44 | ( "x", 20), 45 | ( "int*", 8 ), 46 | ( "short*", 8 ), 47 | ( "long*", 8 ), 48 | ( "char*", 8 ), 49 | ( "float*", 8 ), 50 | ( "double*", 8 ), 51 | ( "unsigned int*", 8 ), 52 | ( "long long*", 8 ), 53 | ( "uint8_t*", 8 ), 54 | ( "uint16_t*", 8 ), 55 | ( "uint32_t*", 8 ), 56 | ( "uint64_t*", 8 ), 57 | ( "uint_fast8_t*", 8 ), 58 | ( "uint_fast16_t*", 8 ), 59 | ( "uintmax_t*", 8 ), 60 | ( "intmax_t*", 8 ), 61 | ( "__int128*", 8 ), 62 | ( "uint32_t*", 8 ), 63 | ( "uint64_t*", 8 ), 64 | ( "student*", 8 ), 65 | ( "&x", 8) 66 | ] 67 | 68 | for typ in types: 69 | print (expected_output_format % typ) 70 | if (expected_output_format % typ).replace(" ", "") not in lines: 71 | exitWithError("ERROR: couldn't find type %s (or it has the incorrect value) in sizes output" % typ[0]) 72 | 73 | def runAndCheckSwap(): 74 | expected_output = "k = 2, m = 1\n" 75 | output = runAndReadOutput("./swap") 76 | 77 | if output != expected_output: 78 | exitWithError('ERROR: actual output: "%s", expected "%s"' % (output, expected_output)) 79 | 80 | def build(make_arg, filename): 81 | print "\nRunning make %s ... " % make_arg 82 | run(["make", filename]) 83 | print "Ok!" 84 | 85 | print "\nChecking that %s was built ... " % filename 86 | if not os.path.isfile(filename): 87 | exitWithError("ERROR: %s binary missing, did you rename it?" % filename) 88 | print "Ok!" 89 | 90 | 91 | print "Running verifying script ... " 92 | 93 | print "\nChecking that the Makefile exists ... " 94 | if not os.path.isfile('Makefile'): 95 | exitWithError('ERROR: Makefile does not exist.') 96 | print "Good!" 97 | 98 | build("sizes", "sizes") 99 | print "Checking output of sizes ... " 100 | runAndCheckSizes() 101 | print "Ok!" 102 | 103 | build("pointer", "pointer") 104 | run("./pointer") # Run pointer as a sanity check, but there's no output to check 105 | 106 | build("swap", "swap") 107 | print "Checking output of swap ... " 108 | runAndCheckSwap() 109 | print "Ok!" 110 | 111 | print "LGTM" 112 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw1/matrix-multiply/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw1/matrix-multiply/.DS_Store -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw1/matrix-multiply/Makefile: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2012 MIT License by 6.172 Staff 2 | 3 | # List all of your source files here (but not your headers), separated by 4 | # spaces. You'll have to add to this list every time you create a new 5 | # source file. 6 | SRC := testbed.c matrix_multiply.c 7 | 8 | # Set the name of your binary. Change it if you like. 9 | PRODUCT := matrix_multiply 10 | 11 | ################################################################################ 12 | # These configuration options change how your code (listed above) is compiled 13 | # every time you type "make". You may have to change these values to complete 14 | # some assignments; you should also feel free to experiment with them. 15 | ################################################################################ 16 | 17 | # This option sets which compiler your code will be compiled with. Likely 18 | # choices are icc, icpc, gcc, g++, clang 19 | CC := clang 20 | 21 | # These flags will be applied to your code any time it is built. 22 | CFLAGS := -Wall -std=c99 -D_POSIX_C_SOURCE=200809L 23 | 24 | # These flags are applied only if you build your code with "make DEBUG=1". -g 25 | # generates debugging symbols, -DDEBUG defines the preprocessor symbol "DEBUG" 26 | # (so that you can use "#ifdef DEBUG" in your code), and -O0 disables compiler 27 | # optimizations, so that the binary generated more directly corresponds to your 28 | # source code. 29 | CFLAGS_DEBUG := -g -DDEBUG -O0 30 | 31 | # In the release version, we ask for many optimizations; -O3 sets the 32 | # optimization level to three. -DNDEBUG defines the NDEBUG macro, 33 | # which disables assertion checks. 34 | CFLAGS_RELEASE := -O1 -DNDEBUG 35 | 36 | # These flags are used to invoke Clang's address sanitizer. 37 | CFLAGS_ASAN := -O1 -g -fsanitize=address 38 | 39 | # These flags are applied when linking object files together into your binary. 40 | # If you need to link against libraries, add the appropriate flags here. By 41 | # default, your code is linked against the "rt" library with the flag -lrt; 42 | # this library is used by the timing code in the testbed. 43 | LDFLAGS := -lrt -flto -fuse-ld=gold 44 | 45 | ################################################################################ 46 | # You probably won't need to change anything below this line, but if you're 47 | # curious about how makefiles work, or if you'd like to customize the behavior 48 | # of your makefile, go ahead and take a peek. 49 | ################################################################################ 50 | 51 | # You shouldn't need to touch this. This keeps track of whether you are 52 | # building in a release or debug configuration, and sets CFLAGS appropriately. 53 | # (This mechanism is based on one from the original materials for 6.197 by 54 | # Ceryen Tan and Marek Olszewski.) 55 | OLDMODE=$(shell cat .buildmode 2> /dev/null) 56 | ifeq ($(DEBUG),1) 57 | CFLAGS := $(CFLAGS_DEBUG) $(CFLAGS) 58 | ifneq ($(OLDMODE),debug) 59 | $(shell echo debug > .buildmode) 60 | endif 61 | else ifeq ($(ASAN),1) 62 | CFLAGS := $(CFLAGS_ASAN) $(CFLAGS) 63 | LDFLAGS := $(LDFLAGS) -fsanitize=address 64 | ifneq ($(OLDMODE),asan) 65 | $(shell echo asan > .buildmode) 66 | endif 67 | else 68 | CFLAGS := $(CFLAGS_RELEASE) $(CFLAGS) 69 | ifneq ($(OLDMODE),nodebug) 70 | $(shell echo nodebug > .buildmode) 71 | endif 72 | endif 73 | 74 | # When you invoke make without an argument, make behaves as though you had 75 | # typed "make all", and builds whatever you have listed here. (It knows to 76 | # pick "make all" because "all" is the first rule listed.) 77 | all: $(PRODUCT) 78 | 79 | # This special "target" will remove the binary and all intermediate files. 80 | clean:: 81 | rm -f $(OBJ) $(PRODUCT) .buildmode \ 82 | $(addsuffix .gcda, $(basename $(SRC))) \ 83 | $(addsuffix .gcno, $(basename $(SRC))) \ 84 | $(addsuffix .gcov, $(SRC) fasttime.h) 85 | 86 | # This rule generates a list of object names. Each of your source files (but 87 | # not your header files) produces a single object file when it's compiled. In 88 | # a later step, all of those object files are linked together to produce the 89 | # binary that you run. 90 | OBJ = $(addsuffix .o, $(basename $(SRC))) 91 | 92 | # These rules tell make how to automatically generate rules that build the 93 | # appropriate object-file from each of the source files listed in SRC (above). 94 | %.o : %.c .buildmode 95 | $(CC) $(CFLAGS) -c $< -o $@ 96 | %.o : %.cc .buildmode 97 | $(CC) $(CFLAGS) -c $< -o $@ 98 | %.o : %.cpp .buildmode 99 | $(CC) $(CFLAGS) -c $< -o $@ 100 | 101 | # This rule tells make that it can produce your binary by linking together all 102 | # of the object files produced from your source files and any necessary 103 | # libraries. 104 | $(PRODUCT): $(OBJ) .buildmode 105 | $(CC) -o $@ $(OBJ) $(LDFLAGS) 106 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw1/matrix-multiply/fasttime.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 MIT License by 6.172 Staff 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to 6 | * deal in the Software without restriction, including without limitation the 7 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 8 | * sell copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 20 | * IN THE SOFTWARE. 21 | **/ 22 | 23 | #ifndef INCLUDED_FASTTIME_DOT_H 24 | #define INCLUDED_FASTTIME_DOT_H 25 | 26 | #include 27 | 28 | #ifdef __MACH__ 29 | #include // mach_absolute_time 30 | 31 | typedef uint64_t fasttime_t; 32 | 33 | 34 | // Return the current time. 35 | static inline fasttime_t gettime(void) { 36 | return mach_absolute_time(); 37 | } 38 | 39 | // Return the time different between the start and the end, as a float 40 | // in units of seconds. This function does not need to be fast. 41 | // Implementation notes: See 42 | // https://developer.apple.com/library/mac/qa/qa1398/_index.html 43 | static inline double tdiff(fasttime_t start, fasttime_t end) { 44 | static mach_timebase_info_data_t timebase; 45 | int r = mach_timebase_info(&timebase); 46 | assert(r == 0); 47 | fasttime_t elapsed = end-start; 48 | double ns = (double)elapsed * timebase.numer / timebase.denom; 49 | return ns*1e-9; 50 | } 51 | 52 | static inline unsigned int random_seed_from_clock(void) { 53 | fasttime_t now = gettime(); 54 | return (now & 0xFFFFFFFF) + (now>>32); 55 | } 56 | 57 | #else // LINUX 58 | 59 | // We need _POSIX_C_SOURCE to pick up 'struct timespec' and clock_gettime. 60 | // #define _POSIX_C_SOURCE 200809L 61 | 62 | #include 63 | 64 | typedef struct timespec fasttime_t; 65 | 66 | // Return the current time. 67 | static inline fasttime_t gettime(void) { 68 | struct timespec s; 69 | #ifdef NDEBUG 70 | clock_gettime(CLOCK_MONOTONIC, &s); 71 | #else 72 | int r = clock_gettime(CLOCK_MONOTONIC, &s); 73 | assert(r == 0); 74 | #endif 75 | return s; 76 | } 77 | 78 | // Return the time different between the start and the end, as a float 79 | // in units of seconds. This function does not need to be fast. 80 | static inline double tdiff(fasttime_t start, fasttime_t end) { 81 | return end.tv_sec - start.tv_sec + 1e-9*(end.tv_nsec - start.tv_nsec); 82 | } 83 | 84 | static inline unsigned int random_seed_from_clock(void) { 85 | fasttime_t now = gettime(); 86 | return now.tv_sec + now.tv_nsec; 87 | } 88 | 89 | // Poison these symbols to help find portability problems. 90 | int clock_gettime(clockid_t, struct timespec *) __attribute__((deprecated)); 91 | time_t time(time_t *) __attribute__((deprecated)); 92 | 93 | #endif // LINUX 94 | 95 | #endif // INCLUDED_FASTTIME_DOT_H 96 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw1/matrix-multiply/matrix_multiply.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2012 MIT License by 6.172 Staff 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to 6 | * deal in the Software without restriction, including without limitation the 7 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 8 | * sell copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 20 | * IN THE SOFTWARE. 21 | **/ 22 | 23 | #include "./matrix_multiply.h" 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | 35 | // #include "./tbassert.h" 36 | 37 | // Allocates a row-by-cols matrix and returns it 38 | matrix* make_matrix(int rows, int cols) { 39 | matrix* new_matrix = malloc(sizeof(matrix)); 40 | 41 | // Set the number of rows and columns 42 | new_matrix->rows = rows; 43 | new_matrix->cols = cols; 44 | 45 | // Allocate a buffer big enough to hold the matrix. 46 | new_matrix->values = (int**)malloc(sizeof(int*) * rows); 47 | for (int i = 0; i < rows; i++) { 48 | new_matrix->values[i] = (int*)malloc(sizeof(int) * cols); 49 | } 50 | 51 | return new_matrix; 52 | } 53 | 54 | // Frees an allocated matrix 55 | void free_matrix(matrix* m) { 56 | for (int i = 0; i < m->rows; i++) { 57 | free(m->values[i]); 58 | } 59 | free(m->values); 60 | free(m); 61 | } 62 | 63 | // Print matrix 64 | void print_matrix(const matrix* m) { 65 | printf("------------\n"); 66 | for (int i = 0; i < m->rows; i++) { 67 | for (int j = 0; j < m->cols; j++) { 68 | printf(" %3d ", m->values[i][j]); 69 | } 70 | printf("\n"); 71 | } 72 | printf("------------\n"); 73 | } 74 | 75 | 76 | // Multiply matrix A*B, store result in C. 77 | int matrix_multiply_run(const matrix* A, const matrix* B, matrix* C) { 78 | /* 79 | tbassert(A->cols == B->rows, 80 | "A->cols = %d, B->rows = %d\n", A->cols, B->rows); 81 | tbassert(A->rows == C->rows, 82 | "A->rows = %d, C->rows = %d\n", A->rows, C->rows); 83 | tbassert(B->cols == C->cols, 84 | "B->cols = %d, C->cols = %d\n", B->cols, C->cols); 85 | */ 86 | 87 | for (int i = 0; i < A->rows; i++) { 88 | for (int j = 0; j < B->cols; j++) { 89 | for (int k = 0; k < A->cols; k++) { 90 | C->values[i][j] += A->values[i][k] * B->values[k][j]; 91 | } 92 | } 93 | } 94 | 95 | return 0; 96 | } 97 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw1/matrix-multiply/matrix_multiply.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2012 MIT License by 6.172 Staff 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to 6 | * deal in the Software without restriction, including without limitation the 7 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 8 | * sell copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 20 | * IN THE SOFTWARE. 21 | **/ 22 | 23 | 24 | /** 25 | * Matrix Multiply 26 | * 27 | * 28 | * Declarations here are your API specification -- we expect that your 29 | * code defines and correctly implements these functions and does not modify 30 | * the way the reference implementation uses variables mentioned here, 31 | * so that we may call them from testbed.c and any other testing software 32 | * that we write! 33 | * 34 | * Deviating from this API may cause your program to fail all of our tests. 35 | **/ 36 | 37 | #ifndef MATRIX_MULTIPLY_H_INCLUDED 38 | 39 | #define MATRIX_MULTIPLY_H_INCLUDED 40 | 41 | typedef struct { 42 | int rows; 43 | int cols; 44 | int** values; 45 | } matrix; 46 | 47 | // Multiply matrix A*B, store result in C. 48 | int matrix_multiply_run(const matrix* A, const matrix* B, matrix* C); 49 | 50 | // Allocates a row-by-cols matrix and returns it 51 | matrix* make_matrix(int rows, int cols); 52 | 53 | // Frees an allocated matrix 54 | void free_matrix(matrix* m); 55 | 56 | // Print matrix 57 | void print_matrix(const matrix* m); 58 | #endif // MATRIX_MULTIPLY_H_INCLUDED 59 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw1/matrix-multiply/tbassert.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 MIT License by Tao B. Schardl 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to 6 | * deal in the Software without restriction, including without limitation the 7 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 8 | * sell copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 20 | * IN THE SOFTWARE. 21 | **/ 22 | 23 | #ifndef _TBASSERT_H_ 24 | #define _TBASSERT_H_ 1 25 | 26 | #ifndef NDEBUG 27 | 28 | /************************************************************************** 29 | * Library of debugging macros. 30 | *************************************************************************/ 31 | 32 | #include 33 | #include 34 | 35 | // Print a message to STREAM when DEBUG = 1. This macro takes the 36 | // same arguments as FPRINTF(). 37 | #define DEBUG_FPRINTF(STREAM, ...) \ 38 | do { \ 39 | fprintf(STREAM, "%s:%d (%s) ", \ 40 | __FILE__, __LINE__, __PRETTY_FUNCTION__); \ 41 | fprintf(STREAM, __VA_ARGS__); \ 42 | } while (0) 43 | 44 | // Print a message to STDERR when DEBUG = 1. This macro takes the 45 | // same arguments as PRINTF(). 46 | #define DEBUG_EPRINTF(...) DEBUG_FPRINTF(stderr, __VA_ARGS__); 47 | 48 | // If PREDICATE is true, do nothing. Otherwise, print an error with 49 | // the specified message to STDERR. This macro only operates when 50 | // DEBUG = 1. This macro takes a PREDICATE to evaluate followed by 51 | // the standard arguments to PRINTF(). 52 | #define DEBUG_ASSERT(PREDICATE, ...) \ 53 | do { \ 54 | if (!(PREDICATE)) { \ 55 | fprintf(stderr, "%s:%d (%s) Assertion " #PREDICATE " failed: ", \ 56 | __FILE__, __LINE__, __PRETTY_FUNCTION__); \ 57 | fprintf(stderr, __VA_ARGS__); \ 58 | abort(); \ 59 | } \ 60 | } while (0) 61 | 62 | #define tbassert DEBUG_ASSERT 63 | 64 | #else 65 | 66 | #define DEBUG_PRINTF(...) // Nothing. 67 | #define DEBUG_EPRINTF(...) // Nothing. 68 | #define DEBUG_ASSERT(...) // Nothing. 69 | #define tbassert(...) // Nothing. 70 | 71 | #endif 72 | 73 | #endif // _TBASSERT_H_ 74 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw1/matrix-multiply/testbed.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2012 MIT License by 6.172 Staff 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to 6 | * deal in the Software without restriction, including without limitation the 7 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 8 | * sell copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 20 | * IN THE SOFTWARE. 21 | **/ 22 | 23 | 24 | /** 25 | * testbed.c: 26 | * 27 | * This file runs your code, timing its execution and printing out the result. 28 | **/ 29 | 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | 36 | #include "./fasttime.h" 37 | #include "./matrix_multiply.h" 38 | 39 | 40 | int main(int argc, char** argv) { 41 | int optchar = 0; 42 | int show_usec = 0; 43 | int should_print = 0; 44 | int use_zero_matrix = 0; 45 | 46 | // Always use the same seed, so that our tests are repeatable. 47 | unsigned int randomSeed = 1; 48 | 49 | matrix* A; 50 | matrix* B; 51 | matrix* C; 52 | 53 | const int kMatrixSize = 4; 54 | 55 | 56 | // Parse command line arguments 57 | while ((optchar = getopt(argc, argv, "upz")) != -1) { 58 | switch (optchar) { 59 | case 'u': 60 | show_usec = 1; 61 | break; 62 | case 'p': 63 | should_print = 1; 64 | break; 65 | case 'z': 66 | use_zero_matrix = 1; 67 | break; 68 | default: 69 | printf("Ignoring unrecognized option: %c\n", optchar); 70 | continue; 71 | } 72 | } 73 | 74 | // This is a trick to make the memory bug leads to a wrong output. 75 | int size = sizeof(int) * 4; 76 | int* temp[20]; 77 | 78 | for (int i = 0; i < 20; i++) { 79 | temp[i] = (int*)malloc(size); 80 | memset(temp[i], 1, size); 81 | } 82 | int total = 0; 83 | for (int i = 0; i < 20; i++) { 84 | for (int j = 0; j < 4; j++) { 85 | total += temp[i][j]; 86 | } 87 | } 88 | if (!total) printf("Trick to stop mallocs from being optimized out."); 89 | for (int i = 0; i < 20; i++) { 90 | free(temp[i]); 91 | } 92 | 93 | fprintf(stderr, "Setup\n"); 94 | 95 | A = make_matrix(kMatrixSize, kMatrixSize+1); 96 | B = make_matrix(kMatrixSize, kMatrixSize); 97 | C = make_matrix(kMatrixSize, kMatrixSize); 98 | 99 | if (use_zero_matrix) { 100 | for (int i = 0; i < A->rows; i++) { 101 | for (int j = 0; j < A->cols; j++) { 102 | A->values[i][j] = 0; 103 | } 104 | } 105 | for (int i = 0; i < B->rows; i++) { 106 | for (int j = 0; j < B->cols; j++) { 107 | B->values[i][j] = 0; 108 | } 109 | } 110 | } else { 111 | for (int i = 0; i < A->rows; i++) { 112 | for (int j = 0; j < A->cols; j++) { 113 | A->values[i][j] = rand_r(&randomSeed) % 10; 114 | } 115 | } 116 | for (int i = 0; i < B->rows; i++) { 117 | for (int j = 0; j < B->cols; j++) { 118 | B->values[i][j] = rand_r(&randomSeed) % 10; 119 | } 120 | } 121 | } 122 | 123 | if (should_print) { 124 | printf("Matrix A: \n"); 125 | print_matrix(A); 126 | 127 | printf("Matrix B: \n"); 128 | print_matrix(B); 129 | } 130 | 131 | fprintf(stderr, "Running matrix_multiply_run()...\n"); 132 | 133 | fasttime_t time1 = gettime(); 134 | matrix_multiply_run(A, B, C); 135 | fasttime_t time2 = gettime(); 136 | 137 | if (should_print) { 138 | printf("---- RESULTS ----\n"); 139 | printf("Result: \n"); 140 | print_matrix(C); 141 | printf("---- END RESULTS ----\n"); 142 | } 143 | 144 | if (show_usec) { 145 | double elapsed = tdiff(time1, time2); 146 | printf("Elapsed execution time: %f usec\n", 147 | elapsed * (1000.0 * 1000.0)); 148 | } else { 149 | double elapsed = tdiff(time1, time2); 150 | printf("Elapsed execution time: %f sec\n", elapsed); 151 | } 152 | 153 | return 0; 154 | } 155 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/__MACOSX/._homework: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw2/__MACOSX/._homework -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/__MACOSX/._recitation: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw2/__MACOSX/._recitation -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/__MACOSX/homework/._Makefile: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw2/__MACOSX/homework/._Makefile -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/__MACOSX/homework/._fasttime.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw2/__MACOSX/homework/._fasttime.h -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/__MACOSX/homework/._isort.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw2/__MACOSX/homework/._isort.c -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/__MACOSX/homework/._main.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw2/__MACOSX/homework/._main.c -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/__MACOSX/homework/._sort.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw2/__MACOSX/homework/._sort.h -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/__MACOSX/homework/._sort_a.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw2/__MACOSX/homework/._sort_a.c -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/__MACOSX/homework/._sort_c.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw2/__MACOSX/homework/._sort_c.c -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/__MACOSX/homework/._sort_f.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw2/__MACOSX/homework/._sort_f.c -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/__MACOSX/homework/._sort_i.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw2/__MACOSX/homework/._sort_i.c -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/__MACOSX/homework/._sort_m.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw2/__MACOSX/homework/._sort_m.c -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/__MACOSX/homework/._sort_p.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw2/__MACOSX/homework/._sort_p.c -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/__MACOSX/homework/._tests.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw2/__MACOSX/homework/._tests.c -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/__MACOSX/homework/._tests.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw2/__MACOSX/homework/._tests.h -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/__MACOSX/homework/._util.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw2/__MACOSX/homework/._util.c -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/__MACOSX/homework/._util.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw2/__MACOSX/homework/._util.h -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/__MACOSX/recitation/._Makefile: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw2/__MACOSX/recitation/._Makefile -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/__MACOSX/recitation/._isort.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw2/__MACOSX/recitation/._isort.c -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/__MACOSX/recitation/._qsort.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw2/__MACOSX/recitation/._qsort.c -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/__MACOSX/recitation/._sum.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw2/__MACOSX/recitation/._sum.c -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/__MACOSX/recitation/._verifier.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw2/__MACOSX/recitation/._verifier.py -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/homework/Makefile: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2012-2013 MIT License by 6.172 Staff 2 | CC := clang 3 | CFLAGS := -g -Wall -std=gnu99 -gdwarf-3 -always-inline 4 | LDFLAGS := -lrt -lm 5 | # You may add new files to the list of COMMON_SRC below 6 | COMMON_SRC := tests.c util.c isort.c sort_a.c sort_c.c sort_i.c sort_p.c sort_m.c sort_f.c 7 | COMMON_HEADERS := fasttime.h util.h tests.h 8 | TARGET := sort 9 | 10 | ifeq ($(DEBUG),1) 11 | CFLAGS := -DDEBUG -O0 $(CFLAGS) 12 | else 13 | CFLAGS := -O3 -DNDEBUG $(CFLAGS) 14 | endif 15 | CFLAGS := $(CFLAGS) 16 | 17 | all: $(TARGET) 18 | 19 | sort: main.c $(COMMON_SRC) $(COMMON_HEADERS) Makefile 20 | $(CC) main.c $(COMMON_SRC) $(CFLAGS) $(LDFLAGS) -o sort 21 | 22 | lint: 23 | python clint.py *.[ch] 24 | # run sort on input sizes around a power of 2 25 | SIZE=131072 26 | run: sort 27 | @echo "\n\nSorting 2^17 - 1 elements" 28 | ./sort 524287 1 29 | @echo "\n\nSorting 2^17 elements" 30 | ./sort 524288 1 31 | @echo "\n\nSorting 2^17 + 1 elements" 32 | ./sort 524289 1 33 | 34 | small:SIZE=32 35 | small:sort 36 | @echo "\n\nSorting 2^5 printed" 37 | ./sort -p ${SIZE} 1 38 | 39 | # echo $[1<<22] 40 | huge:SIZE=262144 41 | huge:sort 42 | ./sort ${SIZE} 2 43 | 44 | stat:sort 45 | valgrind --tool=cachegrind --branch-sim=yes ./sort ${SIZE} 1 46 | 47 | aws_stat:sort 48 | awsrun valgrind --tool=cachegrind --branch-sim=yes ./sort ${SIZE} 1 49 | 50 | # kcachegrind to display trace output 51 | # callgrind:SIZE=131072 52 | # callgrind:sort 53 | # valgrind --tool=callgrind --vgdb=no --trace-children=no -v ./sort ${SIZE} 1 54 | # @echo 55 | # @echo Now start kcachegrind\& 56 | 57 | # cq_callgrind:SIZE=131072 58 | # cq_callgrind:sort 59 | # cqrun valgrind --tool=callgrind --vgdb=no --trace-children=no -v ./sort ${SIZE} 1 60 | # @echo 61 | # @echo Now start kcachegrind\& 62 | 63 | memcheck:sort 64 | valgrind --leak-check=full ./sort 1000 1 65 | 66 | pre-submit: 67 | make DEBUG=1 clean small 68 | make memcheck 69 | make clean small 70 | make memcheck 71 | 72 | # remove all targets as well as output generated by PNQ 73 | clean:: 74 | rm -f ./sort *.std* *.gcov *.gcda *.gcno default.profraw 75 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/homework/fasttime.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 MIT License by 6.172 Staff 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to 6 | * deal in the Software without restriction, including without limitation the 7 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 8 | * sell copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 20 | * IN THE SOFTWARE. 21 | **/ 22 | 23 | #ifndef INCLUDED_FASTTIME_DOT_H 24 | #define INCLUDED_FASTTIME_DOT_H 25 | 26 | #define _POSIX_C_SOURCE 200809L 27 | 28 | #include 29 | 30 | #ifdef __MACH__ 31 | #include // mach_absolute_time 32 | 33 | typedef uint64_t fasttime_t; 34 | 35 | 36 | // Return the current time. 37 | static inline fasttime_t gettime(void) { 38 | return mach_absolute_time(); 39 | } 40 | 41 | // Return the time different between the start and the end, as a float 42 | // in units of seconds. This function does not need to be fast. 43 | // Implementation notes: See 44 | // https://developer.apple.com/library/mac/qa/qa1398/_index.html 45 | static inline double tdiff(fasttime_t start, fasttime_t end) { 46 | static mach_timebase_info_data_t timebase; 47 | int r = mach_timebase_info(&timebase); 48 | assert(r == 0); 49 | fasttime_t elapsed = end - start; 50 | double ns = (double)elapsed * timebase.numer / timebase.denom; 51 | return ns * 1e-9; 52 | } 53 | 54 | static inline unsigned int random_seed_from_clock(void) { 55 | fasttime_t now = gettime(); 56 | return (now & 0xFFFFFFFF) + (now >> 32); 57 | } 58 | 59 | #else // LINUX 60 | 61 | // We need _POSIX_C_SOURCE to pick up 'struct timespec' and clock_gettime. 62 | // #define _POSIX_C_SOURCE 200809L 63 | 64 | #include 65 | 66 | typedef struct timespec fasttime_t; 67 | 68 | // Return the current time. 69 | static inline fasttime_t gettime(void) { 70 | struct timespec s; 71 | #ifdef NDEBUG 72 | clock_gettime(CLOCK_MONOTONIC, &s); 73 | #else 74 | int r = clock_gettime(CLOCK_MONOTONIC, &s); 75 | assert(r == 0); 76 | #endif 77 | return s; 78 | } 79 | 80 | // Return the time different between the start and the end, as a float 81 | // in units of seconds. This function does not need to be fast. 82 | static inline double tdiff(fasttime_t start, fasttime_t end) { 83 | return end.tv_sec - start.tv_sec + 1e-9 * (end.tv_nsec - start.tv_nsec); 84 | } 85 | 86 | static inline unsigned int random_seed_from_clock(void) { 87 | fasttime_t now = gettime(); 88 | return now.tv_sec + now.tv_nsec; 89 | } 90 | 91 | // Poison these symbols to help find portability problems. 92 | int clock_gettime(clockid_t, struct timespec*) __attribute__((deprecated)); 93 | time_t time(time_t*) __attribute__((deprecated)); 94 | 95 | #endif // LINUX 96 | 97 | #endif // INCLUDED_FASTTIME_DOT_H 98 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/homework/isort.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2012 MIT License by 6.172 Staff 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to 6 | * deal in the Software without restriction, including without limitation the 7 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 8 | * sell copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 20 | * IN THE SOFTWARE. 21 | **/ 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | // Typedefs 28 | typedef uint32_t data_t; 29 | 30 | // Insertion sort, sorting the array between begin and end, inclusive 31 | void isort(data_t* begin, data_t* end) { 32 | data_t* cur = begin + 1; 33 | while (cur <= end) { 34 | data_t val = *cur; 35 | data_t* index = cur - 1; 36 | 37 | while (index >= begin && *index > val) { 38 | *(index + 1) = *index; 39 | index--; 40 | } 41 | 42 | *(index + 1) = val; 43 | cur++; 44 | } 45 | } 46 | 47 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/homework/main.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2013-2014 MIT License by 6.172 Staff 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to 6 | * deal in the Software without restriction, including without limitation the 7 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 8 | * sell copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 20 | * IN THE SOFTWARE. 21 | **/ 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #include "./fasttime.h" 29 | #include "./tests.h" 30 | 31 | // Extern variables 32 | extern test_case test_cases[]; 33 | 34 | 35 | static void run_test_suite(int start_idx, int printFlag, int N, int R, 36 | struct testFunc_t* testFunc, int numFunc) { 37 | for (int i = 0; test_cases[i] != NULL; i++) { 38 | if (i < start_idx) { 39 | continue; 40 | } 41 | fprintf(stderr, "\nRunning test #%d...\n", i); 42 | (*test_cases[i])(printFlag, N, R, testFunc, numFunc); 43 | } 44 | fprintf(stderr, "Done testing.\n"); 45 | } 46 | 47 | 48 | extern void sort_a(data_t*, int, int); 49 | extern void sort_i(data_t*, int, int); 50 | extern void sort_p(data_t*, int, int); 51 | extern void sort_c(data_t*, int, int); 52 | extern void sort_m(data_t*, int, int); 53 | extern void sort_f(data_t*, int, int); 54 | 55 | int main(int argc, char** argv) { 56 | int N, R, optchar, printFlag = 0; 57 | unsigned int seed = 0; 58 | 59 | // an array of struct testFunc_t indicating the sort functions to test 60 | // the struct contains two fields --- the function pointer to the sort function 61 | // and its name (for printing) 62 | struct testFunc_t testFunc[] = { 63 | {&sort_a, "sort_a\t\t"}, 64 | {&sort_a, "sort_a repeated\t"}, 65 | //{&sort_i, "sort_i\t\t"}, 66 | //{&sort_p, "sort_p\t\t"}, 67 | //{&sort_c, "sort_c\t\t"}, 68 | //{&sort_m, "sort_m\t\t"}, 69 | //{&sort_f, "sort_f\t\t"}, 70 | }; 71 | const int kNumOfFunc = sizeof(testFunc) / sizeof(testFunc[0]); 72 | 73 | // process command line options 74 | while ((optchar = getopt(argc, argv, "s:p")) != -1) { 75 | switch (optchar) { 76 | case 's': 77 | seed = (unsigned int) atoi(optarg); 78 | printf("Using user-provided seed: %u\n", seed); 79 | srand(seed); 80 | break; 81 | case 'p': 82 | printFlag = 1; 83 | break; 84 | default: 85 | printf("Ignoring unrecognized option: %c\n", optchar); 86 | continue; 87 | } 88 | } 89 | 90 | // shift remaining arguments over 91 | int remaining_args = argc - optind; 92 | for (int i = 1; i <= remaining_args; ++i) { 93 | argv[i] = argv[i + optind - 1]; 94 | } 95 | 96 | // check to make sure number of arguments is correct 97 | if (remaining_args != 2) { 98 | printf("Usage: %s [-p] \n", argv[0]); 99 | printf("-p : print before/after arrays\n"); 100 | exit(-1); 101 | } 102 | 103 | N = atoi(argv[1]); 104 | R = atoi(argv[2]); 105 | 106 | run_test_suite(0, printFlag, N, R, testFunc, kNumOfFunc); 107 | 108 | return 0; 109 | } 110 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/homework/sort.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012 MIT License by 6.172 Staff 2 | 3 | #include "./util.h" 4 | 5 | #ifndef SORT_H 6 | #define SORT_H 7 | 8 | void sort_a(data_t* left, int p, int r); 9 | void sort_i(data_t* left, int p, int r); 10 | void sort_p(data_t* left, int p, int r); 11 | void sort_c(data_t* left, int p, int r); 12 | void sort_m(data_t* left, int p, int r); 13 | void sort_f(data_t* left, int p, int r); 14 | 15 | #endif // SORT_H 16 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/homework/sort_a.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2012 MIT License by 6.172 Staff 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to 6 | * deal in the Software without restriction, including without limitation the 7 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 8 | * sell copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 20 | * IN THE SOFTWARE. 21 | **/ 22 | 23 | 24 | #include "./util.h" 25 | 26 | // Function prototypes 27 | static void merge_a(data_t* A, int p, int q, int r); 28 | static void copy_a(data_t* source, data_t* dest, int n); 29 | 30 | // A basic merge sort routine that sorts the subarray A[p..r] 31 | void sort_a(data_t* A, int p, int r) { 32 | assert(A); 33 | if (p < r) { 34 | int q = (p + r) / 2; 35 | sort_a(A, p, q); 36 | sort_a(A, q + 1, r); 37 | merge_a(A, p, q, r); 38 | } 39 | } 40 | 41 | // A merge routine. Merges the sub-arrays A [p..q] and A [q + 1..r]. 42 | // Uses two arrays 'left' and 'right' in the merge operation. 43 | static void merge_a(data_t* A, int p, int q, int r) { 44 | assert(A); 45 | assert(p <= q); 46 | assert((q + 1) <= r); 47 | int n1 = q - p + 1; 48 | int n2 = r - q; 49 | 50 | data_t* left = 0, * right = 0; 51 | mem_alloc(&left, n1 + 1); 52 | mem_alloc(&right, n2 + 1); 53 | if (left == NULL || right == NULL) { 54 | mem_free(&left); 55 | mem_free(&right); 56 | return; 57 | } 58 | 59 | copy_a(&(A[p]), left, n1); 60 | copy_a(&(A[q + 1]), right, n2); 61 | left[n1] = UINT_MAX; 62 | right[n2] = UINT_MAX; 63 | 64 | int i = 0; 65 | int j = 0; 66 | 67 | for (int k = p; k <= r; k++) { 68 | if (left[i] <= right[j]) { 69 | A[k] = left[i]; 70 | i++; 71 | } else { 72 | A[k] = right[j]; 73 | j++; 74 | } 75 | } 76 | mem_free(&left); 77 | mem_free(&right); 78 | } 79 | 80 | static void copy_a(data_t* source, data_t* dest, int n) { 81 | assert(dest); 82 | assert(source); 83 | 84 | for (int i = 0 ; i < n ; i++) { 85 | dest[i] = source[i]; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/homework/sort_c.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2012 MIT License by 6.172 Staff 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to 6 | * deal in the Software without restriction, including without limitation the 7 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 8 | * sell copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 20 | * IN THE SOFTWARE. 21 | **/ 22 | 23 | 24 | #include "./util.h" 25 | 26 | void sort_c(data_t* A, int p, int r) { 27 | printf("Unimplemented!\n"); 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/homework/sort_f.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2012 MIT License by 6.172 Staff 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to 6 | * deal in the Software without restriction, including without limitation the 7 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 8 | * sell copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 20 | * IN THE SOFTWARE. 21 | **/ 22 | 23 | 24 | #include "./util.h" 25 | 26 | void sort_f(data_t* A, int p, int r) { 27 | printf("Unimplemented!\n"); 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/homework/sort_i.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2012 MIT License by 6.172 Staff 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to 6 | * deal in the Software without restriction, including without limitation the 7 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 8 | * sell copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 20 | * IN THE SOFTWARE. 21 | **/ 22 | 23 | 24 | #include "./util.h" 25 | 26 | void sort_i(data_t* A, int p, int r) { 27 | printf("Unimplemented!\n"); 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/homework/sort_m.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2012 MIT License by 6.172 Staff 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to 6 | * deal in the Software without restriction, including without limitation the 7 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 8 | * sell copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 20 | * IN THE SOFTWARE. 21 | **/ 22 | 23 | 24 | #include "./util.h" 25 | 26 | void sort_m(data_t* A, int p, int r) { 27 | printf("Unimplemented!\n"); 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/homework/sort_p.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2012 MIT License by 6.172 Staff 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to 6 | * deal in the Software without restriction, including without limitation the 7 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 8 | * sell copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 20 | * IN THE SOFTWARE. 21 | **/ 22 | 23 | 24 | #include "./util.h" 25 | 26 | void sort_p(data_t* A, int p, int r) { 27 | printf("Unimplemented!\n"); 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/homework/tests.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2012 MIT License by 6.172 Staff 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to 6 | * deal in the Software without restriction, including without limitation the 7 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 8 | * sell copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 20 | * IN THE SOFTWARE. 21 | **/ 22 | 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | #include "./fasttime.h" 32 | #include "./tests.h" 33 | 34 | // Call TEST_PASS() from your test cases to mark a test as successful 35 | #define TEST_PASS() TEST_PASS_WITH_NAME(__func__, __LINE__) 36 | 37 | #define TEST_PASS_WITH_NAME(name, line) \ 38 | fprintf(stderr, " --> %s at line %d: PASS\n", (name), (line)) 39 | 40 | // Call TEST_FAIL from your test cases to mark a test as failed. TEST_FAIL 41 | // should print a meaningful message with the reason that the test failed. 42 | // 43 | // Calling it is just like calling printf(). 44 | #define TEST_FAIL(failure_msg, args...) \ 45 | TEST_FAIL_WITH_NAME(__func__, __LINE__, failure_msg, ##args) 46 | 47 | #define TEST_FAIL_WITH_NAME(name, line, failure_msg, args...) do { \ 48 | fprintf(stderr, " --> %s at line %d: FAIL\n Reason:", (name), (line)); \ 49 | fprintf(stderr, (failure_msg), ## args); \ 50 | fprintf(stderr, "\n"); \ 51 | } while (0) 52 | 53 | 54 | 55 | 56 | /* Some global variables to make it easier to run individual tests. */ 57 | // static int test_verbose = 1 ; 58 | static unsigned int randomSeed = 1; 59 | 60 | static inline void display_array(data_t* data, int N) { 61 | // display array 62 | for (int i = 0; i < N; i++) { 63 | printf("%d ", data[i]); 64 | } 65 | printf("\n"); 66 | } 67 | 68 | static inline void copy_data(data_t* data, data_t* data_bcup, int N) { 69 | // copy data_bcup to data 70 | for (int i = 0 ; i < N ; i++) { 71 | *data++ = *data_bcup++; 72 | } 73 | } 74 | 75 | static inline int post_process(data_t* data, data_t* data_bcup, int N, 76 | int printFlag, char* name, int begin, int end) { 77 | int result = 1; 78 | if (printFlag) { 79 | printf("%s: ", name); 80 | printf("Data after sort\n"); 81 | display_array(data, N); 82 | } 83 | 84 | // check if the array is unchanged from data[0..begin-1] 85 | for (int i = 0 ; i < begin ; i++) { 86 | if (data[i] != data_bcup[i]) { 87 | printf("%s: ", name); 88 | TEST_FAIL("Array outside sort boundary changed!\n"); 89 | result = 0; 90 | break; 91 | } 92 | } 93 | 94 | // check if sub-array is sorted 95 | for (int i = begin + 1 ; i < end + 1 ; i++) { 96 | if (data[i - 1] > data[i]) { 97 | printf("%s: ", name); 98 | TEST_FAIL("Arrays are sorted: NO!\n"); 99 | result = 0; 100 | break; 101 | } 102 | } 103 | 104 | // check if the array is unchanged from data[end+1..N-1] 105 | for (int i = end + 1 ; i < N ; i++) { 106 | if (data[i] != data_bcup[i]) { 107 | printf("%s: ", name); 108 | TEST_FAIL("Array outside sort boundary changed!\n"); 109 | result = 0; 110 | break; 111 | } 112 | } 113 | copy_data(data, data_bcup, N); 114 | return result; 115 | } 116 | 117 | static 118 | void 119 | init_data(data_t* data, int N, int randomPrefix, int invertedSuffix) { 120 | // initialize data with randomPrefix random numbers 121 | assert(randomPrefix <= N); 122 | /* random prefix */ 123 | for (int i = 0; i < randomPrefix; i++) { 124 | data[i] = rand_r(&randomSeed) % RANGE; 125 | } 126 | /* sorted or inverted suffix - duplicates are OK */ 127 | for (int i = randomPrefix; i < N; i++) { 128 | if (invertedSuffix) { 129 | data[i] = (N - i) % RANGE; // inverted 130 | } else { 131 | data[i] = i % RANGE; // sorted array 132 | } 133 | } 134 | } 135 | 136 | 137 | struct dataGenerator_t { 138 | void (*generate)(data_t*, int); 139 | char* name; 140 | }; 141 | 142 | void all_random(data_t* data, int N) { 143 | init_data(data, N, N, 0); 144 | } 145 | 146 | void all_inverted(data_t* data, int N) { 147 | init_data(data, N, 0, 1 /* inverted */); 148 | } 149 | 150 | struct dataGenerator_t dataGen[] = { 151 | {all_random, "random"}, 152 | {all_inverted, "inverted"}, 153 | }; 154 | 155 | static void test_correctness(int printFlag, int N, int R, 156 | struct testFunc_t* testFunc, int numFunc) { 157 | fasttime_t time1, time2; 158 | data_t* data, *data_bcup; 159 | int success = 1; 160 | 161 | float* sum_time = (float*) alloca(numFunc * sizeof(float)); 162 | for (int i = 0; i < numFunc; i++) { 163 | sum_time[i] = 0; 164 | } 165 | 166 | // allocate memory 167 | data = (data_t*) malloc(N * sizeof(data_t)); 168 | data_bcup = (data_t*) malloc(N * sizeof(data_t)); 169 | 170 | if (data == NULL || data_bcup == NULL) { 171 | printf("Error: not enough memory\n"); 172 | free(data); 173 | free(data_bcup); 174 | exit(-1); 175 | } 176 | 177 | // Initialize elapsed time counters to 0. 178 | for (int k = 0; k < numFunc; k++) { 179 | sum_time[k] = 0; 180 | } 181 | 182 | // repeat for each array kind 183 | for (int gen = 0; 184 | gen < sizeof(dataGen) / sizeof(dataGen[0]); 185 | gen++) { 186 | printf("Generating %s array of %d elements\n", 187 | dataGen[gen].name, N); 188 | // repeat for each trial 189 | for (int j = 0; j < R; j++) { 190 | // generate new data for each trial 191 | dataGen[gen].generate(data, N); 192 | 193 | if (printFlag) { 194 | printf("Data before sort\n"); 195 | display_array(data, N); 196 | } 197 | for (int i = 0; i < N; i++) { 198 | data_bcup[i] = data[i]; 199 | } 200 | 201 | for (int k = 0; k < numFunc; k++) { 202 | time1 = gettime(); 203 | testFunc[k].func(data, 0, N - 1); 204 | time2 = gettime(); 205 | 206 | sum_time[k] += tdiff(time1, time2); 207 | success &= post_process(data, data_bcup, N, printFlag, testFunc[k].name, 0, N - 1); 208 | 209 | if (!success) { 210 | break; 211 | } 212 | } 213 | } 214 | 215 | if (success) { 216 | printf("Arrays are sorted: yes\n"); 217 | TEST_PASS(); 218 | // report average execution time over R runs. 219 | for (int k = 0; k < numFunc; k++) { 220 | float avgRuntime = R > 0 ? sum_time[k] / R : 0; 221 | printf("%s: Elapsed execution time: %f sec\n", testFunc[k].name, avgRuntime); 222 | } 223 | } 224 | } 225 | 226 | free(data); 227 | free(data_bcup); 228 | return; 229 | } 230 | 231 | static void test_zero_element(int printFlag, int N, int R, 232 | struct testFunc_t* testFunc, int numFunc) { 233 | int success = 1; 234 | for (int i = 0; i < numFunc; i++) { 235 | data_t data[] = {0, 0, 0}; 236 | testFunc[i].func(&data[1], 0, 0); 237 | if (data[0] != 0 && data[1] != 0 && data[2] != 0) { 238 | printf("Error: %s failed to sort array with zero element\n", 239 | testFunc[i].name); 240 | success = 0; 241 | } 242 | } 243 | 244 | if (success) { 245 | TEST_PASS(); 246 | } else { 247 | TEST_FAIL("Sorting array with zero element failed"); 248 | } 249 | } 250 | 251 | static void test_one_element(int printFlag, int N, int R, 252 | struct testFunc_t* testFunc, int numFunc) { 253 | int success = 1; 254 | for (int i = 0; i < numFunc; i++) { 255 | data_t data[] = {0, 1, 0}; 256 | testFunc[i].func(&data[1], 0, 0); 257 | if (data[0] != 0 && data[2] != 0) { 258 | // Can't test data[1] == 1 because of final part of homework. 259 | printf("Error: %s failed to sort array with one element\n", 260 | testFunc[i].name); 261 | success = 0; 262 | } 263 | } 264 | 265 | if (success) { 266 | TEST_PASS(); 267 | } else { 268 | TEST_FAIL("Sorting array with one element failed"); 269 | } 270 | } 271 | 272 | void test_subarray(int printFlag, int N, int R, 273 | struct testFunc_t* testFunc, int numFunc) { 274 | data_t* data, *data_bcup; 275 | int success = 1; 276 | 277 | // allocate memory 278 | data = (data_t*) malloc(N * sizeof(data_t)); 279 | data_bcup = (data_t*) malloc(N * sizeof(data_t)); 280 | 281 | if (data == NULL || data_bcup == NULL) { 282 | printf("Error: not enough memory\n"); 283 | free(data); 284 | free(data_bcup); 285 | exit(-1); 286 | } 287 | 288 | // initialize data with random numbers 289 | for (int i = 0; i < N; i++) { 290 | data[i] = rand_r(&randomSeed); 291 | data_bcup[i] = data[i]; 292 | } 293 | if (printFlag) { 294 | printf("Data before sort\n"); 295 | display_array(data, N); 296 | } 297 | int begin = rand_r(&randomSeed) % N; 298 | int end = N - 1 - begin; 299 | if (begin > end) { 300 | int temp = begin; 301 | begin = end; 302 | end = temp; 303 | } 304 | 305 | printf("Sorting subarray A[%d..%d]\n", begin, end); 306 | for (int i = 0; i < numFunc; i++) { 307 | testFunc[i].func(data, begin, end); 308 | success &= post_process(data, data_bcup, N, printFlag, testFunc[i].name, begin, end); 309 | } 310 | 311 | if (success) { 312 | printf("Arrays are sorted: yes\n"); 313 | TEST_PASS(); 314 | } else { 315 | TEST_FAIL("Sorting subarray failed"); 316 | } 317 | 318 | free(data); 319 | free(data_bcup); 320 | return; 321 | } 322 | 323 | test_case test_cases[] = { 324 | test_correctness, 325 | test_zero_element, 326 | test_one_element, 327 | // test_subarray, 328 | // ADD YOUR TEST CASES HERE 329 | NULL // This marks the end of all test cases. Don't change this! 330 | }; 331 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/homework/tests.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012 MIT License by 6.172 Staff 2 | 3 | #ifndef TESTS_H 4 | #define TESTS_H 5 | 6 | #include "./util.h" 7 | 8 | struct testFunc_t { 9 | void (*func)(data_t*, int, int); 10 | char* name; 11 | }; 12 | 13 | #define RANGE_BITS 31 14 | #define RANGE (1U << RANGE_BITS) 15 | 16 | typedef void (*test_case)(int printFlag, int N, int R, struct testFunc_t* testFunc, int numFunc); 17 | 18 | #endif // TESTS_H 19 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/homework/util.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2012 MIT License by 6.172 Staff 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to 6 | * deal in the Software without restriction, including without limitation the 7 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 8 | * sell copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 20 | * IN THE SOFTWARE. 21 | **/ 22 | 23 | 24 | #include "./util.h" 25 | 26 | 27 | void mem_alloc(data_t** space, int size) { 28 | *space = (data_t*) malloc(sizeof(data_t) * size); 29 | if (*space == NULL) { 30 | printf("out of memory...\n"); 31 | } 32 | } 33 | 34 | void mem_free(data_t** space) { 35 | free(*space); 36 | *space = 0; 37 | } 38 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/homework/util.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012 MIT License by 6.172 Staff 2 | 3 | #ifndef UTIL_H 4 | #define UTIL_H 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | typedef uint32_t data_t; 13 | 14 | void mem_alloc(data_t** space, int size); 15 | void mem_free(data_t** space); 16 | 17 | #endif // UTIL_H 18 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/recitation/Makefile: -------------------------------------------------------------------------------- 1 | CC := clang 2 | 3 | ifeq ($(DEBUG),1) 4 | CFLAGS := -Wall -O0 -g 5 | else 6 | CFLAGS := -Wall -O1 -DNDEBUG 7 | endif 8 | 9 | LDFLAGS := -lrt 10 | 11 | all: isort sum 12 | 13 | isort.o: isort.c 14 | $(CC) $(CFLAGS) -c isort.c 15 | 16 | qsort.o: qsort.c 17 | $(CC) $(CFLAGS) -c qsort.c 18 | 19 | isort: isort.o qsort.o 20 | $(CC) -o isort isort.o qsort.o $(LDFLAGS) 21 | 22 | sum.o: sum.c 23 | $(CC) $(CFLAGS) -c sum.c 24 | 25 | sum: sum.o 26 | $(CC) -o sum sum.o $(LDFLAGS) 27 | 28 | clean: 29 | rm -f isort sum *.o *.gcda *.gcno *.gcov perf.data */perf.data cachegrind.out.* 30 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/recitation/isort.c: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012 MIT License by 6.172 Staff 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | /* Typedefs */ 8 | 9 | typedef uint32_t data_t; 10 | 11 | extern void quickSortIterative(data_t arr[], int l, int h); 12 | 13 | /* Insertion sort */ 14 | void isort(data_t* left, data_t* right) { 15 | data_t* cur = left + 1; 16 | while (cur <= right) { 17 | data_t val = *cur; 18 | data_t* index = cur - 1; 19 | 20 | while (index >= left && *index > val) { 21 | *(index + 1) = *index; 22 | index--; 23 | } 24 | 25 | *(index + 1) = val; 26 | cur++; 27 | } 28 | } 29 | 30 | int main(int argc, char* argv[]) { 31 | if (argc != 3) { 32 | printf("Error: wrong number of arguments.\n"); 33 | exit(-1); 34 | } 35 | int N = atoi(argv[1]); 36 | int K = atoi(argv[2]); 37 | unsigned int seed = 42; 38 | printf("Sorting %d values...\n", N); 39 | data_t* data = (data_t*) malloc(N * sizeof(data_t)); 40 | if (data == NULL) { 41 | free(data); 42 | printf("Error: not enough memory\n"); 43 | exit(-1); 44 | } 45 | 46 | int i, j; 47 | for (j = 0; j < K ; j++) { 48 | for (i = 0; i < N; i++) { 49 | data[i] = rand_r(&seed); 50 | // printf("%d ", data[i]); 51 | } 52 | // printf("\n"); 53 | 54 | isort(data, data + N - 1); 55 | //quickSortIterative(data, 0, N); 56 | /*for (i = 0; i < N; i++) { 57 | printf("%d ", data[i]); 58 | } 59 | printf("\n");*/ 60 | } 61 | free(data); 62 | printf("Done!\n"); 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/recitation/qsort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /* Typedefs */ 6 | 7 | typedef uint32_t data_t; 8 | 9 | // A utility function to swap two elements 10 | void swap(data_t* a, data_t* b) { 11 | data_t t = *a; 12 | *a = *b; 13 | *b = t; 14 | } 15 | 16 | /* This function is same in both iterative and recursive*/ 17 | int partition(data_t arr[], int l, int h) { 18 | data_t x = arr[h]; 19 | int i = (l - 1); 20 | 21 | for (int j = l; j <= h - 1; j++) { 22 | if (arr[j] <= x) { 23 | i++; 24 | swap(&arr[i], &arr[j]); 25 | } 26 | } 27 | swap(&arr[i + 1], &arr[h]); 28 | return (i + 1); 29 | } 30 | 31 | /* arr[] --> Array to be sorted, 32 | l --> Starting index, 33 | h --> Ending index */ 34 | void quickSortIterative(data_t arr[], int l, int h) { 35 | // Create an auxiliary stack 36 | int stack[ h - l + 1 ]; 37 | 38 | // initialize top of stack 39 | int top = -1; 40 | 41 | // push initial values of l and h to stack 42 | stack[ ++top ] = l; 43 | stack[ ++top ] = h; 44 | 45 | // Keep popping from stack while is not empty 46 | while (top >= 0) { 47 | // Pop h and l 48 | h = stack[ top-- ]; 49 | l = stack[ top-- ]; 50 | 51 | // Set pivot element at its correct position 52 | // in sorted array 53 | int p = partition(arr, l, h); 54 | 55 | // If there are elements on left side of pivot, 56 | // then push left side to stack 57 | if (p - 1 > l) { 58 | stack[ ++top ] = l; 59 | stack[ ++top ] = p - 1; 60 | } 61 | 62 | // If there are elements on right side of pivot, 63 | // then push right side to stack 64 | if (p + 1 < h) { 65 | stack[ ++top ] = p + 1; 66 | stack[ ++top ] = h; 67 | } 68 | } 69 | } 70 | 71 | 72 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/recitation/sum.c: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2012 MIT License by 6.172 Staff 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | typedef uint32_t data_t; 8 | const int U = 10000000; // size of the array. 10 million vals ~= 40MB 9 | const int N = 100000000; // number of searches to perform 10 | 11 | int main() { 12 | data_t* data = (data_t*) malloc(U * sizeof(data_t)); 13 | if (data == NULL) { 14 | free(data); 15 | printf("Error: not enough memory\n"); 16 | exit(-1); 17 | } 18 | 19 | // fill up the array with sequential (sorted) values. 20 | int i; 21 | for (i = 0; i < U; i++) { 22 | data[i] = i; 23 | } 24 | 25 | printf("Allocated array of size %d\n", U); 26 | printf("Summing %d random values...\n", N); 27 | 28 | data_t val = 0; 29 | data_t seed = 42; 30 | for (i = 0; i < N; i++) { 31 | int l = rand_r(&seed) % U; 32 | val = (val + data[l]); 33 | } 34 | 35 | free(data); 36 | printf("Done. Value = %d\n", val); 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw2/recitation/verifier.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | import subprocess, os, sys, re 3 | 4 | def exitWithError(error): 5 | print error 6 | sys.exit(1) 7 | 8 | def runAndReadOutput(args): 9 | if args is str: 10 | args = [args] 11 | try: 12 | return subprocess.check_output(args) 13 | except subprocess.CalledProcessError as e: 14 | exitWithError("ERROR: runtime error with %s" % str(args)) 15 | 16 | def run(path): runAndReadOutput(path) 17 | 18 | def runAndCheckSizes(): 19 | output = runAndReadOutput("./sizes") 20 | expected_output_format = "size of %s : %d bytes" 21 | 22 | lines = set([x.strip() for x in output.strip().lower().split('\n')]) 23 | types = [ 24 | ( "int", 4 ), 25 | ( "short", 2 ), 26 | ( "long", 8 ), 27 | ( "char", 1 ), 28 | ( "float", 4 ), 29 | ( "double", 8 ), 30 | ( "unsigned int", 4 ), 31 | ( "long long", 8 ), 32 | ( "uint8_t", 1 ), 33 | ( "uint16_t", 2 ), 34 | ( "uint32_t", 4 ), 35 | ( "uint64_t", 8 ), 36 | ( "uint_fast8_t", 1 ), 37 | ( "uint_fast16_t", 8 ), 38 | ( "uintmax_t", 8 ), 39 | ( "intmax_t", 8 ), 40 | ( "__int128", 16 ), 41 | ( "uint32_t", 4 ), 42 | ( "uint64_t", 8 ), 43 | ( "student", 8 ), 44 | ] 45 | 46 | for typ in types: 47 | print (expected_output_format % typ) 48 | if (expected_output_format % typ) not in lines: 49 | exitWithError("ERROR: couldn't find type %s (or it has the incorrect value) in sizes output" % typ[0]) 50 | 51 | def runAndCheckSwap(): 52 | expected_output = "k = 2, m = 1\n" 53 | output = runAndReadOutput("./swap") 54 | 55 | if output != expected_output: 56 | exitWithError('ERROR: actual output: "%s", expected "%s"' % (output, expected_output)) 57 | 58 | def build(make_arg, filename): 59 | print "\nRunning make %s ... " % make_arg 60 | run(["make", filename]) 61 | print "Ok!" 62 | 63 | print "\nChecking that %s was built ... " % filename 64 | if not os.path.isfile(filename): 65 | exitWithError("ERROR: %s binary missing, did you rename it?" % filename) 66 | print "Ok!" 67 | 68 | 69 | print "Running verifying script ... " 70 | 71 | print "\nChecking that the Makefile exists ... " 72 | if not os.path.isfile('Makefile'): 73 | exitWithError('ERROR: Makefile does not exist.') 74 | print "Good!" 75 | 76 | build("sizes", "sizes") 77 | print "Checking output of sizes ... " 78 | runAndCheckSizes() 79 | print "Ok!" 80 | 81 | build("pointer", "pointer") 82 | run("./pointer") # Run pointer as a sanity check, but there's no output to check 83 | 84 | build("swap", "swap") 85 | print "Checking output of swap ... " 86 | runAndCheckSwap() 87 | print "Ok!" 88 | 89 | print "LGTM" 90 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw3/__MACOSX/._homework3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw3/__MACOSX/._homework3 -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw3/__MACOSX/._recitation3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw3/__MACOSX/._recitation3 -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw3/__MACOSX/homework3/._.DS_Store: -------------------------------------------------------------------------------- 1 | Mac OS X  2Fx @ATTRxx -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw3/__MACOSX/homework3/._Makefile: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw3/__MACOSX/homework3/._Makefile -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw3/__MACOSX/homework3/._fasttime.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw3/__MACOSX/homework3/._fasttime.h -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw3/__MACOSX/homework3/._loop.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw3/__MACOSX/homework3/._loop.c -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw3/__MACOSX/recitation3/._.DS_Store: -------------------------------------------------------------------------------- 1 | Mac OS X  2Fx @ATTRxx -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw3/__MACOSX/recitation3/._.cflags: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw3/__MACOSX/recitation3/._.cflags -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw3/__MACOSX/recitation3/._Makefile: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw3/__MACOSX/recitation3/._Makefile -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw3/__MACOSX/recitation3/._example1.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw3/__MACOSX/recitation3/._example1.c -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw3/__MACOSX/recitation3/._example1.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw3/__MACOSX/recitation3/._example1.o -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw3/__MACOSX/recitation3/._example2.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw3/__MACOSX/recitation3/._example2.c -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw3/__MACOSX/recitation3/._example2.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw3/__MACOSX/recitation3/._example2.o -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw3/__MACOSX/recitation3/._example3.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw3/__MACOSX/recitation3/._example3.c -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw3/__MACOSX/recitation3/._example3.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw3/__MACOSX/recitation3/._example3.o -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw3/__MACOSX/recitation3/._example4.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw3/__MACOSX/recitation3/._example4.c -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw3/__MACOSX/recitation3/._example4.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw3/__MACOSX/recitation3/._example4.o -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw3/homework3/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw3/homework3/.DS_Store -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw3/homework3/Makefile: -------------------------------------------------------------------------------- 1 | CC := clang 2 | 3 | EXTRA_CFLAGS ?= 4 | CFLAGS := -Wall -std=gnu99 -g 5 | 6 | ifeq ($(DEBUG),1) 7 | CFLAGS += -O0 8 | else 9 | CFLAGS += -O3 -DNDEBUG 10 | endif 11 | 12 | ifeq ($(VECTORIZE),1) 13 | CFLAGS += -Rpass=loop-vectorize -Rpass-missed=loop-vectorize -ffast-math 14 | else 15 | CFLAGS += -fno-vectorize 16 | endif 17 | 18 | ifeq ($(ASSEMBLE),1) 19 | CFLAGS += -S 20 | endif 21 | 22 | ifeq ($(AVX2),1) 23 | CFLAGS += -mavx2 24 | endif 25 | 26 | CFLAGS += $(EXTRA_CFLAGS) 27 | 28 | LDFLAGS := -lrt 29 | 30 | # You shouldn't need to touch this. This keeps track of whether or 31 | # not you've changed CFLAGS. 32 | OLD_CFLAGS := $(shell cat .cflags 2> /dev/null) 33 | ifneq ($(CFLAGS),$(OLD_CFLAGS)) 34 | .cflags:: 35 | @echo "$(CFLAGS)" > .cflags 36 | endif 37 | 38 | 39 | all: loop 40 | 41 | %.o: %.c .cflags 42 | $(CC) $(CFLAGS) -c $< 43 | 44 | loop: loop.o 45 | $(CC) -o loop $^ $(LDFLAGS) 46 | 47 | clean:: 48 | rm -f loop *.o *.s .cflags perf.data */perf.data 49 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw3/homework3/fasttime.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 MIT License by 6.172 Staff 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to 6 | * deal in the Software without restriction, including without limitation the 7 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 8 | * sell copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 20 | * IN THE SOFTWARE. 21 | **/ 22 | 23 | #ifndef INCLUDED_FASTTIME_DOT_H 24 | #define INCLUDED_FASTTIME_DOT_H 25 | 26 | #define _POSIX_C_SOURCE 200809L 27 | 28 | #include 29 | 30 | #ifdef __MACH__ 31 | #include // mach_absolute_time 32 | 33 | typedef uint64_t fasttime_t; 34 | 35 | 36 | // Return the current time. 37 | static inline fasttime_t gettime(void) { 38 | return mach_absolute_time(); 39 | } 40 | 41 | // Return the time different between the start and the end, as a float 42 | // in units of seconds. This function does not need to be fast. 43 | // Implementation notes: See 44 | // https://developer.apple.com/library/mac/qa/qa1398/_index.html 45 | static inline double tdiff(fasttime_t start, fasttime_t end) { 46 | static mach_timebase_info_data_t timebase; 47 | int r = mach_timebase_info(&timebase); 48 | assert(r == 0); 49 | fasttime_t elapsed = end-start; 50 | double ns = (double)elapsed * timebase.numer / timebase.denom; 51 | return ns*1e-9; 52 | } 53 | 54 | static inline unsigned int random_seed_from_clock(void) { 55 | fasttime_t now = gettime(); 56 | return (now & 0xFFFFFFFF) + (now>>32); 57 | } 58 | 59 | #else // LINUX 60 | 61 | // We need _POSIX_C_SOURCE to pick up 'struct timespec' and clock_gettime. 62 | // #define _POSIX_C_SOURCE 200809L 63 | 64 | #include 65 | 66 | typedef struct timespec fasttime_t; 67 | 68 | // Return the current time. 69 | static inline fasttime_t gettime(void) { 70 | struct timespec s; 71 | #ifdef NDEBUG 72 | clock_gettime(CLOCK_MONOTONIC, &s); 73 | #else 74 | int r = clock_gettime(CLOCK_MONOTONIC, &s); 75 | assert(r == 0); 76 | #endif 77 | return s; 78 | } 79 | 80 | // Return the time different between the start and the end, as a float 81 | // in units of seconds. This function does not need to be fast. 82 | static inline double tdiff(fasttime_t start, fasttime_t end) { 83 | return end.tv_sec - start.tv_sec + 1e-9*(end.tv_nsec - start.tv_nsec); 84 | } 85 | 86 | static inline unsigned int random_seed_from_clock(void) { 87 | fasttime_t now = gettime(); 88 | return now.tv_sec + now.tv_nsec; 89 | } 90 | 91 | // Poison these symbols to help find portability problems. 92 | int clock_gettime(clockid_t, struct timespec *) __attribute__((deprecated)); 93 | time_t time(time_t *) __attribute__((deprecated)); 94 | 95 | #endif // LINUX 96 | 97 | #endif // INCLUDED_FASTTIME_DOT_H 98 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw3/homework3/loop.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2014 MIT License by 6.172 Staff 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to 6 | * deal in the Software without restriction, including without limitation the 7 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 8 | * sell copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 20 | * IN THE SOFTWARE. 21 | **/ 22 | 23 | #include 24 | #include 25 | #include 26 | #include "./fasttime.h" 27 | 28 | // N is small enough so that 3 arrays of size N fit into the AWS machine 29 | // level 1 caches (which are 32 KB each, as seen by running `lscpu`) 30 | #define N 1024 31 | 32 | // Run for multiple experiments to reduce measurement error on gettime(). 33 | #define I 100000 34 | 35 | // Which operations are vectorizable? 36 | // Guarding it with #ifndef allows passing -D"__OP__=$value" on the 37 | // command line 38 | #ifndef __OP__ 39 | #define __OP__ + 40 | #endif 41 | #ifndef __TYPE__ 42 | #define __TYPE__ uint32_t 43 | #endif 44 | 45 | // Define a way of automatically converting __OP__ and __TYPE__ into string literals 46 | #define stringify(V) _stringify(V) 47 | #define _stringify(V) #V 48 | 49 | int main(int argc, char *argv[]) { 50 | __TYPE__ A[N]; 51 | __TYPE__ B[N]; 52 | __TYPE__ C[N]; 53 | __TYPE__ total = 0; 54 | 55 | int i, j; 56 | unsigned int seed = 0; 57 | 58 | // Touch each element in each array before we start the timed part 59 | // of execution. This operation brings all arrays into the level 1 60 | // cache and gives us a 'cleaner' view of speedup from vectorization. 61 | for (j = 0; j < N; j++) { 62 | A[j] = 0; // 0 was chosen arbitrarily 63 | B[j] = 0; 64 | C[j] = 0; 65 | } 66 | 67 | fasttime_t time1 = gettime(); 68 | 69 | for (i = 0; i < I; i++) { 70 | for (j = 0; j < N; j++) { 71 | C[j] = A[j] __OP__ B[j]; 72 | } 73 | } 74 | 75 | fasttime_t time2 = gettime(); 76 | 77 | // Forces the compiler to not prune away any loop operations 78 | total += C[rand_r(&seed) % N]; 79 | 80 | double elapsedf = tdiff(time1, time2); 81 | // C concatenates adjacent string literals. We take advantage of 82 | // this and include a print-out of __OP__ and __TYPE__ 83 | printf("Elapsed execution time: %f sec; N: %d, I: %d," 84 | " __OP__: %s, __TYPE__: %s\n", 85 | elapsedf, N, I, stringify(__OP__), stringify(__TYPE__)); 86 | 87 | return total; 88 | } 89 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw3/recitation3/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw3/recitation3/.DS_Store -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw3/recitation3/.cflags: -------------------------------------------------------------------------------- 1 | -Wall -g -std=gnu99 -O3 -fno-vectorize 2 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw3/recitation3/Makefile: -------------------------------------------------------------------------------- 1 | CC := clang 2 | 3 | TARGETS = example1 example2 example3 example4 4 | 5 | OTHER_CFLAGS ?= 6 | CFLAGS := -Wall -g -std=gnu99 7 | 8 | ifeq ($(DEBUG),1) 9 | CFLAGS += -O0 10 | else 11 | CFLAGS += -O3 12 | endif 13 | 14 | V ?= 1 15 | 16 | ifeq ($(VECTORIZE),1) 17 | #CFLAGS += -ftree-vectorizer-verbose=$(V) 18 | CFLAGS += -Rpass=loop-vectorize -Rpass-missed=loop-vectorize 19 | else 20 | CFLAGS += -fno-vectorize 21 | endif 22 | 23 | ifeq ($(ASSEMBLE),1) 24 | CFLAGS += -S 25 | endif 26 | 27 | ifeq ($(AVX2),1) 28 | CFLAGS += -mavx2 29 | endif 30 | 31 | CFLAGS += $(OTHER_CFLAGS) 32 | 33 | LDFLAGS := -lrt 34 | 35 | # You shouldn't need to touch this. This keeps track of whether or 36 | # not you've changed CFLAGS. 37 | OLD_CFLAGS := $(shell cat .cflags 2> /dev/null) 38 | ifneq ($(CFLAGS),$(OLD_CFLAGS)) 39 | .cflags:: 40 | @echo "$(CFLAGS)" > $@ 41 | endif 42 | 43 | 44 | all: $(TARGETS:=.o) 45 | 46 | %.o: %.c .cflags 47 | $(CC) $(CFLAGS) -c $< 48 | 49 | %.lst: %.c .cflags 50 | $(CC) $(CFLAGS) -Wa,-aslh -c $< > $@ 51 | 52 | clean:: 53 | rm -rf 54 | rm -f *.o *.s .cflags perf.data *.lst 55 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw3/recitation3/example1.c: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2015 MIT License by 6.172 Staff 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #define SIZE (1L << 16) 8 | 9 | void test(uint8_t * a, uint8_t * b) { 10 | uint64_t i; 11 | 12 | for (i = 0; i < SIZE; i++) { 13 | a[i] += b[i]; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw3/recitation3/example1.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw3/recitation3/example1.o -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw3/recitation3/example2.c: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2015 MIT License by 6.172 Staff 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #define SIZE (1L << 16) 8 | 9 | void test(uint8_t * restrict a, uint8_t * restrict b) { 10 | uint64_t i; 11 | 12 | uint8_t * x = __builtin_assume_aligned(a, 16); 13 | uint8_t * y = __builtin_assume_aligned(b, 16); 14 | 15 | for (i = 0; i < SIZE; i++) { 16 | /* max() */ 17 | if (y[i] > x[i]) x[i] = y[i]; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw3/recitation3/example2.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw3/recitation3/example2.o -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw3/recitation3/example3.c: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2015 MIT License by 6.172 Staff 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #define SIZE (1L << 16) 8 | 9 | void test(uint8_t * restrict a, uint8_t * restrict b) { 10 | uint64_t i; 11 | 12 | for (i = 0; i < SIZE; i++) { 13 | a[i] = b[i + 1]; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw3/recitation3/example3.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw3/recitation3/example3.o -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw3/recitation3/example4.c: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2015 MIT License by 6.172 Staff 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #define SIZE (1L << 16) 9 | 10 | double test(double * restrict a) { 11 | size_t i; 12 | 13 | double *x = __builtin_assume_aligned(a, 16); 14 | 15 | double y = 0; 16 | 17 | for (i = 0; i < SIZE; i++) { 18 | y += x[i]; 19 | } 20 | return y; 21 | } 22 | 23 | int main() { 24 | double a[SIZE]; 25 | for (int i = 0; i < SIZE; i++) { 26 | a[i] = 1.0/(i*1.0+1.0); 27 | } 28 | double sum = test(a); 29 | printf("The decimal floating point sum result is: %f\n", sum); 30 | printf("The raw floating point sum result is: %a\n", sum); 31 | } 32 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw3/recitation3/example4.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw3/recitation3/example4.o -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw7/Makefile: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2017 MIT License by 6.172 Staff 2 | 3 | CC := clang 4 | CFLAGS := -O3 5 | CILKFLAGS := -fcilkplus 6 | 7 | LDFLAGS := -flto 8 | TARGET := fib_serial fib_cilk 9 | LDFLAGS_TOOL := -flto 10 | all: null-tool.o func-analysis-tool.o linreg-tool.o func-analysis.o linreg.o fib_serial fib_cilk 11 | 12 | null-tool.o: null-tool.c 13 | $(CC) -o null-tool.o $(LDFLAGS_TOOL) -c null-tool.c $(CFLAGS) 14 | 15 | func-analysis-tool.o : null-tool.o func-analysis.c 16 | $(CC) -o func-analysis-tool.o $(LDFLAGS_TOOL) -c func-analysis.c $(CFLAGS) 17 | 18 | linreg-tool.o : null-tool.o linreg.c 19 | $(CC) -o linreg-tool.o $(LDFLAGS_TOOL) -c linreg.c $(CFLAGS) 20 | 21 | func-analysis.o: func-analysis-tool.o 22 | linreg.o: linreg-tool.o 23 | 24 | func-analysis.o linreg.o: null-tool.o 25 | llvm-link -o $@ $(patsubst %.o, %-tool.o, $@) null-tool.o 26 | 27 | CSI_FLAG := 28 | ifeq ($(CSI),1) 29 | CSI_FLAG += -fcsi 30 | LDFLAGS += -fuse-ld=gold -lclang_rt.csi-x86_64 31 | endif 32 | 33 | fib_serial.o: fib_serial.c 34 | $(CC) -o fib_serial.o $(LDFLAGS_TOOL) $(CSI_FLAG) -c fib_serial.c $(CFLAGS) -g 35 | 36 | fib_cilk.o: fib_cilk.c 37 | $(CC) -o fib_cilk.o $(LDFLAGS_TOOL) $(CSI_FLAG) $(CILKFLAGS) -c fib_cilk.c $(CFLAGS) -g 38 | 39 | TOOL := 40 | ifeq ($(FUNCANALYSIS),1) 41 | TOOL += func-analysis.o 42 | endif 43 | ifeq ($(LINREG),1) 44 | TOOL += linreg.o 45 | endif 46 | 47 | fib_serial: fib_serial.o $(TOOL) 48 | $(CC) -o fib_serial fib_serial.o $(TOOL) $(CFLAGS) $(LDFLAGS) 49 | 50 | fib_cilk: fib_cilk.o $(TOOL) 51 | $(CC) -o fib_cilk fib_cilk.o $(TOOL) $(CFLAGS) $(CILKFLAGS) $(LDFLAGS) 52 | 53 | clean:: 54 | rm -f fib_serial fib_cilk .buildmode *.o *.bc *.a 55 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw7/__MACOSX/._Makefile: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw7/__MACOSX/._Makefile -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw7/__MACOSX/._fib_cilk.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw7/__MACOSX/._fib_cilk.c -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw7/__MACOSX/._fib_serial.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw7/__MACOSX/._fib_serial.c -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw7/__MACOSX/._func-analysis.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw7/__MACOSX/._func-analysis.c -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw7/__MACOSX/._linreg.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw7/__MACOSX/._linreg.c -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw7/__MACOSX/._null-tool.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw7/__MACOSX/._null-tool.c -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw7/fib_cilk.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | // Exponential-time recursive function to compute the nth Fibonacci 8 | // number. This function spawns recursive calls to fib until n is 9 | // smaller than the specified base-case size, at which point it 10 | // replaces the spawn with a simple recursive call. 11 | int fib(int n, int base) { 12 | if (n < 2) return n; 13 | int x, y; 14 | if (n < base) 15 | x = fib(n - 1, base); 16 | else 17 | x = cilk_spawn fib(n - 1, base); 18 | y = fib(n - 2, base); 19 | cilk_sync; 20 | return (x + y); 21 | } 22 | 23 | int main(int argc, char *argv[]) { 24 | int n = 35; 25 | int base = 2; 26 | 27 | // Parse the first command-line argument to be the Fibonacci number 28 | // to compute. 29 | if (argc > 1) 30 | n = atoi(argv[1]); 31 | // Parse the second command-line argument to be the base-case size 32 | // to use. 33 | if (argc > 2) 34 | base = atoi(argv[2]); 35 | 36 | int r = fib(n, base); 37 | 38 | printf("fib(%d) = %d\n", n, r); 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw7/fib_serial.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | // Simple base-case method that computes the nth Fibonacci number 8 | // iteratively. 9 | __attribute__((always_inline)) 10 | int fib_base(int n) { 11 | if (n < 2) return n; 12 | int x0 = 0, x1 = 1; 13 | for (int i = 2; i < n; ++i) { 14 | int tmp = x0; 15 | x0 = x1; 16 | x1 += tmp; 17 | } 18 | return x0 + x1; 19 | } 20 | 21 | // Exponential-time recursive function to compute the nth Fibonacci 22 | // number. This function switches to fib_base when n becomes smaller 23 | // than the base-case size, which is assumed to be at least 2. 24 | int fib(int n, int base) { 25 | if (n < base) return fib_base(n); 26 | int x, y; 27 | x = fib(n - 1, base); 28 | y = fib(n - 2, base); 29 | return (x + y); 30 | } 31 | 32 | int main(int argc, char *argv[]) { 33 | int n = 35; 34 | int base = 2; 35 | 36 | // Parse the first command-line argument to be the Fibonacci number 37 | // to compute. 38 | if (argc > 1) 39 | n = atoi(argv[1]); 40 | // Parse the second command-line argument to be the base-case size 41 | // to use. 42 | if (argc > 2) 43 | base = atoi(argv[2]); 44 | 45 | int r = fib(n, base); 46 | 47 | printf("fib(%d) = %d\n", n, r); 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw7/func-analysis.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // This tool reports the number of times each function in the program 4 | // was executed. 5 | 6 | // TODO: Add global variables for tracking function coverage. 7 | 8 | // TODO: Add a report function. 9 | 10 | void __csi_init() { 11 | // TODO: Register the report function to run at exit. 12 | } 13 | 14 | void __csi_unit_init(const char * const file_name, 15 | const instrumentation_counts_t counts) { 16 | // Expand any data structures to accommodate the additional functions 17 | // in this compilation unit. 18 | 19 | // TODO: Fill this in. 20 | } 21 | 22 | // TODO: Fill in the appropriate hooks (see API reference at the end 23 | // of the handout). 24 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw7/linreg.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void __csi_init() { 4 | // TODO: Fill this in. 5 | } 6 | 7 | void __csi_unit_init(const char * const file_name, 8 | const instrumentation_counts_t counts) { 9 | // TODO: Fill this in. 10 | } 11 | 12 | // TODO: Fill in the appropriate hooks (see API reference at the end 13 | // of the handout). 14 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw7/null-tool.c: -------------------------------------------------------------------------------- 1 | #include "csi.h" 2 | 3 | WEAK void __csi_init() {} 4 | 5 | __attribute__((always_inline)) 6 | WEAK void __csi_unit_init(const char * const file_name, 7 | const instrumentation_counts_t counts) {} 8 | 9 | __attribute__((always_inline)) 10 | WEAK void __csi_before_load(const csi_id_t load_id, 11 | const void *addr, 12 | const int32_t num_bytes, 13 | const load_prop_t prop) {} 14 | 15 | __attribute__((always_inline)) 16 | WEAK void __csi_after_load(const csi_id_t load_id, 17 | const void *addr, 18 | const int32_t num_bytes, 19 | const load_prop_t prop) {} 20 | 21 | __attribute__((always_inline)) 22 | WEAK void __csi_before_store(const csi_id_t store_id, 23 | const void *addr, 24 | const int32_t num_bytes, 25 | const store_prop_t prop) {} 26 | 27 | __attribute__((always_inline)) 28 | WEAK void __csi_after_store(const csi_id_t store_id, 29 | const void *addr, 30 | const int32_t num_bytes, 31 | const store_prop_t prop) {} 32 | 33 | __attribute__((always_inline)) 34 | WEAK void __csi_func_entry(const csi_id_t func_id, const func_prop_t prop) {} 35 | 36 | __attribute__((always_inline)) 37 | WEAK void __csi_func_exit(const csi_id_t func_exit_id, 38 | const csi_id_t func_id, const func_exit_prop_t prop) {} 39 | 40 | __attribute__((always_inline)) 41 | WEAK void __csi_bb_entry(const csi_id_t bb_id, const bb_prop_t prop) {} 42 | 43 | __attribute__((always_inline)) 44 | WEAK void __csi_bb_exit(const csi_id_t bb_id, const bb_prop_t prop) {} 45 | 46 | __attribute__((always_inline)) 47 | WEAK void __csi_before_call(csi_id_t callsite_id, csi_id_t func_id, 48 | const call_prop_t prop) {} 49 | 50 | __attribute__((always_inline)) 51 | WEAK void __csi_after_call(csi_id_t callsite_id, csi_id_t func_id, 52 | const call_prop_t prop) {} 53 | 54 | __attribute__((always_inline)) 55 | WEAK void __csi_detach(const csi_id_t detach_id) {} 56 | 57 | __attribute__((always_inline)) 58 | WEAK void __csi_task(const csi_id_t task_id, const csi_id_t detach_id, 59 | void *sp) {} 60 | 61 | __attribute__((always_inline)) 62 | WEAK void __csi_task_exit(const csi_id_t task_exit_id, 63 | const csi_id_t task_id, 64 | const csi_id_t detach_id) {} 65 | 66 | __attribute__((always_inline)) 67 | WEAK void __csi_detach_continue(const csi_id_t detach_continue_id, 68 | const csi_id_t detach_id) {} 69 | 70 | __attribute__((always_inline)) 71 | WEAK void __csi_sync(const csi_id_t sync_id) {} 72 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw9/Makefile: -------------------------------------------------------------------------------- 1 | all: hashtable undef user 2 | #DBG=-DVERBOSE 3 | CC=clang 4 | CFLAGS=-std=gnu99 -gdwarf-3 -Wall -O1 -fdetach 5 | LDFLAGS += -ldl 6 | REPLAY=0 7 | # non-cilk, single-threaded only record replay support with gdb 7.5 8 | ifeq ($(REPLAY),0) 9 | hashtable: CFLAGS += -fcilkplus -DCILK 10 | hashtable: LDFLAGS +=-lcilkrts 11 | hashlock-unit: CFLAGS += -fcilkplus -DCILK 12 | hashlock-unit: LDFLAGS +=-lcilkrts 13 | endif 14 | hashtable: LDFLAGS += -lpthread 15 | hashlock-unit: LDFLAGS += -lpthread 16 | 17 | hashtable1: ./hashtable 18 | ./hashtable 19 | hashtable10: ./hashtable 20 | bash -c "for((i=0;i<10;i++)) do ./hashtable; done" 21 | hashtable1000: ./hashtable 22 | bash -c "for((i=0;i<1000;i++)) do ./hashtable; done" 23 | 24 | # XXX modify this target so that 1000 runs all succeed 25 | hashtable1000-good: ./hashtable 26 | bash -c "for((i=0;i<1000;i++)) do ./hashtable; done" 27 | # XXX modify this target so the program always fails 28 | hashtable1-bad: ./hashtable 29 | ./hashtable 30 | 31 | hashtable-mt: ./hashtable 32 | ./hashtable 10 33 | hashtable-mt1000: ./hashtable 34 | bash -c "for((i=0;i<1000;i++)) do ./hashtable 10; done" 35 | 36 | whoami: ./user 37 | @./user 38 | 39 | clean: 40 | @-rm -f ./undef ./hashtable ./hashlock-unit ./user 41 | 42 | undef-run: ./undef 43 | ./undef 44 | ./undef 45 | 46 | undef-compare: 47 | make CFLAGS=-O3 clean undef; ./undef 48 | make CFLAGS=-O1 clean undef; ./undef 49 | 50 | undef-noaslr: ./undef 51 | setarch x86_64 -R ./undef 52 | setarch x86_64 -R ./undef 53 | 54 | undef-env: ./undef 55 | setarch x86_64 -R env USER=me ./undef 56 | setarch x86_64 -R env USER=professoramarasinghe ./undef 57 | 58 | undef-all: undef-run undef-compare undef-env undef-noaslr 59 | 60 | hashtable: hashtable.c hashlock.c common.h 61 | $(CC) -o $@ $(DBG) $(CFLAGS) hashtable.c hashlock.c $(LDFLAGS) 62 | 63 | %: %.c 64 | $(CC) -o $@ $(DBG) $(CFLAGS) $^ $(LDFLAGS) 65 | 66 | hashlock-unit: hashlock.c Makefile 67 | $(CC) -o $@ -Wall -g -fdetach -DUNIT_TEST $< $(LDFLAGS) 68 | 69 | testall: hashtable10 hashlock-unit undef-all 70 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw9/__MACOSX/._Makefile: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw9/__MACOSX/._Makefile -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw9/__MACOSX/._common.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw9/__MACOSX/._common.h -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw9/__MACOSX/._hashlock.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw9/__MACOSX/._hashlock.c -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw9/__MACOSX/._hashlock.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw9/__MACOSX/._hashlock.h -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw9/__MACOSX/._hashtable.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw9/__MACOSX/._hashtable.c -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw9/__MACOSX/._undef.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw9/__MACOSX/._undef.c -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw9/__MACOSX/._user.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18_hw9/__MACOSX/._user.c -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw9/common.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2013 MIT License by 6.172 Staff 2 | * 3 | */ 4 | 5 | #ifndef COMMON_H 6 | #define COMMON_H 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | // Atomic: 13 | // prev = *ptr; 14 | // if (*ptr == old) *ptr = _new; 15 | // return prev 16 | static inline uint32_t InterlockedCompareExchange32(volatile uint32_t* ptr, uint32_t _new, 17 | uint32_t old) { 18 | uint32_t prev; 19 | asm volatile("lock;" 20 | "cmpxchgl %1, %2;" 21 | : "=a"(prev) 22 | : "q"(_new), "m"(*ptr), "a"(old) 23 | : "memory"); 24 | return prev; 25 | } 26 | 27 | static inline uint64_t InterlockedCompareExchange64(volatile uint64_t* ptr, uint64_t _new, 28 | uint64_t old) { 29 | uint64_t prev; 30 | asm volatile("lock;" 31 | "cmpxchgq %1, %2;" 32 | : "=a"(prev) 33 | : "q"(_new), "m"(*ptr), "a"(old) 34 | : "memory"); 35 | return prev; 36 | } 37 | 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw9/hashlock.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2013 MIT License by 6.172 Staff 2 | * 3 | * DON'T USE THE FOLLOWING SOFTWARE, IT HAS KNOWN BUGS, AND POSSIBLY 4 | * UNKNOWN BUGS AS WELL. 5 | * 6 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 7 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 8 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 9 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 10 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 11 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 12 | * IN THE SOFTWARE. 13 | * 14 | * Permission is hereby granted, free of charge, to any person obtaining a copy 15 | * of this software and associated documentation files (the "Software"), to 16 | * deal in the Software without restriction, including without limitation the 17 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 18 | * sell copies of the Software, and to permit persons to whom the Software is 19 | * furnished to do so, subject to the following conditions: 20 | * 21 | * The above copyright notice and this permission notice shall be included in 22 | * all copies or substantial portions of the Software. 23 | */ 24 | 25 | #define _POSIX_C_SOURCE 200112L 26 | #define _GNU_SOURCE 27 | 28 | #include 29 | #include 30 | 31 | #define HLOCKS 16 32 | pthread_mutex_t hashlock[HLOCKS]; 33 | #define HASHLOCK_SLOT(l) &hashlock[(l) & (HLOCKS - 1)] 34 | 35 | void hashlock_init() { 36 | int i; 37 | pthread_mutexattr_t recursive; 38 | pthread_mutexattr_init(&recursive); 39 | pthread_mutexattr_settype(&recursive, PTHREAD_MUTEX_RECURSIVE); 40 | for (i = 0; i < HLOCKS; i++) { 41 | pthread_mutex_init(&hashlock[i], &recursive); 42 | } 43 | } 44 | 45 | void hashlock_lock(int l) { 46 | pthread_mutex_lock(HASHLOCK_SLOT(l)); 47 | #ifdef UNIT_TEST 48 | printf("lock %d\n", l); 49 | #endif 50 | } 51 | 52 | void hashlock_unlock(int l) { 53 | pthread_mutex_unlock(HASHLOCK_SLOT(l)); 54 | #ifdef UNIT_TEST 55 | printf("unlock %d\n", l); 56 | #endif 57 | } 58 | 59 | #ifdef UNIT_TEST 60 | int main() { 61 | hashlock_init(); 62 | printf("lock size %ld\n", sizeof(hashlock[0])); 63 | 64 | hashlock_lock(2); 65 | hashlock_lock(2); 66 | hashlock_lock(5); 67 | hashlock_unlock(2); 68 | hashlock_unlock(5); 69 | return 0; 70 | } 71 | #endif 72 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw9/hashlock.h: -------------------------------------------------------------------------------- 1 | #ifndef HASHLOCK_H 2 | #define HASHLOCK_H 3 | 4 | void hashlock_lock(int l); 5 | void hashlock_unlock(int l); 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw9/hashtable.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2013 MIT License by 6.172 Staff 2 | * 3 | * DON'T USE THE FOLLOWING SOFTWARE, IT HAS KNOWN BUGS, AND POSSIBLY 4 | * UNKNOWN BUGS AS WELL. 5 | * 6 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 7 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 8 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 9 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 10 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 11 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 12 | * IN THE SOFTWARE. 13 | * 14 | * Permission is hereby granted, free of charge, to any person obtaining a copy 15 | * of this software and associated documentation files (the "Software"), to 16 | * deal in the Software without restriction, including without limitation the 17 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 18 | * sell copies of the Software, and to permit persons to whom the Software is 19 | * furnished to do so, subject to the following conditions: 20 | * 21 | * The above copyright notice and this permission notice shall be included in 22 | * all copies or substantial portions of the Software. 23 | */ 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include "./hashlock.h" 31 | #include 32 | 33 | #include "./common.h" 34 | 35 | #define HASHBITS 8 36 | #define TABLESIZE (1<> (64 - HASHBITS); 59 | } 60 | 61 | void hashtable_insert(void* p, int size) { 62 | assert(ht.entries < TABLESIZE); 63 | ht.entries++; 64 | 65 | int s = hash_func(p); 66 | /* open addressing with linear probing */ 67 | do { 68 | if (!ht.hashtable[s].ptr) { 69 | ht.hashtable[s].ptr = p; 70 | ht.hashtable[s].size = size; 71 | break; 72 | } 73 | /* conflict, look for next item */ 74 | s++; 75 | } while (1); 76 | } 77 | 78 | void hashtable_lock() { 79 | pthread_mutex_lock(&ht.lock); 80 | } 81 | 82 | void hashtable_unlock() { 83 | pthread_mutex_unlock(&ht.lock); 84 | } 85 | 86 | void hashtable_insert_locked(void* p, int size) { 87 | int s = hash_func(p); 88 | /* open addressing with linear probing */ 89 | hashtable_lock(); 90 | assert(ht.entries < TABLESIZE); 91 | ht.entries++; 92 | 93 | do { 94 | if (!ht.hashtable[s].ptr) { 95 | ht.hashtable[s].ptr = p; 96 | ht.hashtable[s].size = size; 97 | break; 98 | } 99 | /* conflict, look for next item */ 100 | s++; 101 | } while (1); 102 | hashtable_unlock(); 103 | } 104 | 105 | void hashtable_insert_fair(void* p, int size) { 106 | hashtable_lock(); 107 | assert(ht.entries < TABLESIZE); 108 | ht.entries++; 109 | hashtable_unlock(); 110 | 111 | int s = hash_func(p); 112 | /* open addressing with linear probing */ 113 | hashlock_lock(s); 114 | do { 115 | if (!ht.hashtable[s].ptr) { 116 | ht.hashtable[s].ptr = p; 117 | ht.hashtable[s].size = size; 118 | break; 119 | } 120 | int olds = s; 121 | /* conflict, look for next item */ 122 | s++; 123 | /* fair lock, hold the old lock, before grabbing the new one */ 124 | hashlock_lock(s); 125 | hashlock_unlock(olds); 126 | } while (1); 127 | hashlock_unlock(s); 128 | } 129 | 130 | void hashtable_insert_lockless(void* p, int size) { 131 | hashtable_lock(); 132 | assert(ht.entries < TABLESIZE); 133 | ht.entries++; 134 | hashtable_unlock(); 135 | 136 | int s = hash_func(p); 137 | /* open addressing with linear probing */ 138 | do { 139 | if (!ht.hashtable[s].ptr) { 140 | ht.hashtable[s].ptr = p; 141 | ht.hashtable[s].size = size; 142 | break; 143 | } 144 | /* conflict, look for next item */ 145 | s++; 146 | } while (1); 147 | } 148 | 149 | entry_t* hashtable_lookup(void* p) { 150 | size_t s = hash_func(p); 151 | do { 152 | if (p == ht.hashtable[s].ptr) { 153 | return &ht.hashtable[s]; 154 | } 155 | if (ht.hashtable[s].ptr == NULL) { 156 | /* not found */ 157 | return NULL; 158 | } 159 | if (ht.hashtable[s].size == 0) { 160 | /* race on incomplete size, pretend not found */ 161 | return NULL; 162 | } 163 | /* check next */ 164 | s++; 165 | #ifndef STUDENT_VERSION 166 | s %= TABLESIZE; 167 | #endif 168 | } while (1); 169 | } 170 | 171 | void hashtable_dump(void) { 172 | int i; 173 | for (i = 0; i < TABLESIZE; i++) 174 | if (ht.hashtable[i].ptr) { 175 | printf("%d %p %ld\n", i, ht.hashtable[i].ptr, ht.hashtable[i].size); 176 | } 177 | } 178 | 179 | void hashtable_entries(void) { 180 | int i; 181 | int count = 0; 182 | for (i = 0; i < TABLESIZE; i++) { 183 | if (ht.hashtable[i].ptr) { 184 | count++; 185 | } 186 | } 187 | } 188 | 189 | void hashtable_test(int n) { 190 | int i; 191 | for (i = 0; i < n; i++) { 192 | int s = random() & (TABLESIZE - 1); 193 | void* p = ht.hashtable[s].ptr; 194 | void* pl = hashtable_lookup(p); 195 | assert(p == pl); 196 | } 197 | } 198 | 199 | void hashtable_free(void) { 200 | int i; 201 | for (i = 0; i < TABLESIZE; i++) { 202 | if (ht.hashtable[i].ptr) { 203 | free(ht.hashtable[i].ptr); 204 | ht.entries--; 205 | } 206 | } 207 | } 208 | 209 | void hashtable_fill(int n) { 210 | int i; 211 | printf("fill %d\n", n); 212 | for (i = 0; i < n; i++) { 213 | int sz = random() % 1000; 214 | char* p = malloc(sz); 215 | // test one of these below 216 | hashtable_insert(p, sz); 217 | // hashtable_insert_locked(p, sz); 218 | // hashtable_insert_fair(p, sz); 219 | // hashtable_insert_lockless(p, sz); 220 | } 221 | } 222 | 223 | int main(int argc, char* argv[]) { 224 | int n = MAX_ENTRIES / 2; /* 50 % fill ratio */ 225 | 226 | // GCC sometimes gets confused by Cilk 227 | // #pragma GCC diagnostic ignored "-Wmaybe-uninitialized" 228 | 229 | int threads = 1; 230 | if (argc > 1) { 231 | threads = atoi(argv[1]); 232 | } 233 | 234 | unsigned seed = time(NULL); 235 | if (argc > 2) { 236 | seed = atoi(argv[2]); 237 | } 238 | 239 | /* verify hashtable entries are aligned, otherwise even 64bit 240 | * writes/reads won't be guaranteed to be atomic 241 | */ 242 | assert((uintptr_t)ht.hashtable % sizeof(long) == 0); 243 | 244 | srandom(seed); 245 | #ifdef VERBOSE 246 | printf("seed = %d\n", seed); 247 | printf("sizeof entry = %ld\n", sizeof(entry_t)); 248 | printf("&entries = %p &last=%p\n", &ht.entries, &ht.hashtable[TABLESIZE]); 249 | #endif 250 | 251 | #ifdef CILK 252 | int i; 253 | for (i = 1; i < threads; i++) { 254 | cilk_spawn hashtable_fill(n / threads); 255 | } 256 | #endif 257 | hashtable_fill(n / threads); 258 | 259 | #ifdef CILK 260 | cilk_sync; 261 | #endif 262 | 263 | #ifdef VERY_VERBOSE 264 | hashtable_dump(); 265 | #endif 266 | printf("%d entries\n", ht.entries); 267 | hashtable_free(); 268 | printf("%d entries\n", ht.entries); 269 | assert(ht.entries == 0); 270 | return 0; 271 | } 272 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw9/undef.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | int i; 6 | printf("value of i=%d\n", i); 7 | printf("address &i=%p\n", &i); 8 | printf("hash of i=%ld\n", ((uintptr_t)&i) & 127); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18_hw9/user.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2013 MIT License by 6.172 Staff 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include 23 | #include 24 | #include 25 | 26 | #define MAX_HOSTNAME 256 27 | int main(int argc, char* argv[], char* envp[]) { 28 | char* username = getenv("USER"); 29 | if (username == NULL) { 30 | username = ""; 31 | } 32 | printf("%s@", username); 33 | 34 | char hostname[MAX_HOSTNAME]; 35 | gethostname(hostname, sizeof(hostname)); 36 | printf("%s\n", hostname); 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Assignments/MIT6_172F18hw1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18hw1.pdf -------------------------------------------------------------------------------- /Assignments/MIT6_172F18hw10.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18hw10.pdf -------------------------------------------------------------------------------- /Assignments/MIT6_172F18hw2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18hw2.pdf -------------------------------------------------------------------------------- /Assignments/MIT6_172F18hw3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18hw3.pdf -------------------------------------------------------------------------------- /Assignments/MIT6_172F18hw4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18hw4.pdf -------------------------------------------------------------------------------- /Assignments/MIT6_172F18hw5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18hw5.pdf -------------------------------------------------------------------------------- /Assignments/MIT6_172F18hw6.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18hw6.pdf -------------------------------------------------------------------------------- /Assignments/MIT6_172F18hw7.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18hw7.pdf -------------------------------------------------------------------------------- /Assignments/MIT6_172F18hw8.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18hw8.pdf -------------------------------------------------------------------------------- /Assignments/MIT6_172F18hw9.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Assignments/MIT6_172F18hw9.pdf -------------------------------------------------------------------------------- /Lectures/MIT6_172F18_lec1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Lectures/MIT6_172F18_lec1.pdf -------------------------------------------------------------------------------- /Lectures/MIT6_172F18_lec10.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Lectures/MIT6_172F18_lec10.pdf -------------------------------------------------------------------------------- /Lectures/MIT6_172F18_lec11.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Lectures/MIT6_172F18_lec11.pdf -------------------------------------------------------------------------------- /Lectures/MIT6_172F18_lec12.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Lectures/MIT6_172F18_lec12.pdf -------------------------------------------------------------------------------- /Lectures/MIT6_172F18_lec13.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Lectures/MIT6_172F18_lec13.pdf -------------------------------------------------------------------------------- /Lectures/MIT6_172F18_lec14.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Lectures/MIT6_172F18_lec14.pdf -------------------------------------------------------------------------------- /Lectures/MIT6_172F18_lec15.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Lectures/MIT6_172F18_lec15.pdf -------------------------------------------------------------------------------- /Lectures/MIT6_172F18_lec16.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Lectures/MIT6_172F18_lec16.pdf -------------------------------------------------------------------------------- /Lectures/MIT6_172F18_lec17.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Lectures/MIT6_172F18_lec17.pdf -------------------------------------------------------------------------------- /Lectures/MIT6_172F18_lec18.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Lectures/MIT6_172F18_lec18.pdf -------------------------------------------------------------------------------- /Lectures/MIT6_172F18_lec19.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Lectures/MIT6_172F18_lec19.pdf -------------------------------------------------------------------------------- /Lectures/MIT6_172F18_lec2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Lectures/MIT6_172F18_lec2.pdf -------------------------------------------------------------------------------- /Lectures/MIT6_172F18_lec20.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Lectures/MIT6_172F18_lec20.pdf -------------------------------------------------------------------------------- /Lectures/MIT6_172F18_lec21.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Lectures/MIT6_172F18_lec21.pdf -------------------------------------------------------------------------------- /Lectures/MIT6_172F18_lec22.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Lectures/MIT6_172F18_lec22.pdf -------------------------------------------------------------------------------- /Lectures/MIT6_172F18_lec23.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Lectures/MIT6_172F18_lec23.pdf -------------------------------------------------------------------------------- /Lectures/MIT6_172F18_lec3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Lectures/MIT6_172F18_lec3.pdf -------------------------------------------------------------------------------- /Lectures/MIT6_172F18_lec4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Lectures/MIT6_172F18_lec4.pdf -------------------------------------------------------------------------------- /Lectures/MIT6_172F18_lec5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Lectures/MIT6_172F18_lec5.pdf -------------------------------------------------------------------------------- /Lectures/MIT6_172F18_lec6.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Lectures/MIT6_172F18_lec6.pdf -------------------------------------------------------------------------------- /Lectures/MIT6_172F18_lec7.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Lectures/MIT6_172F18_lec7.pdf -------------------------------------------------------------------------------- /Lectures/MIT6_172F18_lec8.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Lectures/MIT6_172F18_lec8.pdf -------------------------------------------------------------------------------- /Lectures/MIT6_172F18_lec9.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SourceryAI/MIT_OpenCourseWare-Performance_Engineering_of_Software_Systems/a5a9ec272b952c1e0e48f470b6399098d7c1b143/Lectures/MIT6_172F18_lec9.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Course Prerequisites 2 | 3 | - [Computation Structures](https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-004-computation-structures-spring-2017/) 4 | - [Introduction to Algorithms](https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/) 5 | - [Elements of Software Construction](https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-005-elements-of-software-construction-fall-2008/) 6 | 7 | ## Lecture Topics (Slides) 8 | 9 | - Introduction & Matrix Multiplication 10 | - Bentley Rules for Optimizing Work 11 | - Bit Hacks 12 | - Assembly Language and Computer Architecture 13 | - C to Assembly 14 | - Multicore Programming 15 | - Races and Parallelism 16 | - Analysis of Multithreaded Algorithms 17 | - What Compilers Can and Cannot Do 18 | - Measurement and Timing 19 | - Storage Allocation 20 | - Parallel Storage Allocation 21 | - The Cilk Runtime System 22 | - Caching and Cache-Efficient Algorithms 23 | - Cache-Oblivious Algorithms 24 | - Nondeterministic Parallel Programming 25 | - Synchronization Without Locks 26 | - Domain Specific Languages and Autotuning 27 | - Leiserchess Codewalk 28 | - Speculative Parallelism & Leiserchess 29 | - Tuning a TSP Algorithm 30 | - Graph Optimization 31 | - High Performance in Dynamic Languages 32 | 33 | ## Supplemental Readings (Not Required) 34 | 35 | - Zhang, Yunming, Mengjiao Yang, et al. "GraphIt: A High-Performance Graph DSL." Proceedings of the ACM on Programming Languages 2 (2018): article no. 121. 36 | - Ansel, Jason, Shoaib Kamil, et al. "OpenTuner: An Extensible Framework for Program Autotuning." Proceedings of the 23rd International Conference on - Parallel Architectures and Compilation (2014): 303–316. 37 | - Leiserson, Charles. "The Cilk++ Concurrency Platform." The Journal of Supercomputing 51, no. 3 (2010): 244–257. 38 | - Leiserson, Charles and Ilya Mirman. "This resource may not render correctly in a screen reader.How to Survive the Multicore Software Revolution (or at - Least Survive the Hype) (PDF - 3.5MB)." 39 | - Frigo, Matteo, Charles Leiserson, and Keith Randall. "The Implementation of the Cilk-5 Multithreaded Language." Proceedings of the 1998 ACM Sigplan - Conference on Programming Language Design and Implementation (PLDI) (1998). 40 | - He, Yuxiong, Charles Leiserson, and William Leiserson. "The Cilkview Scalability Analyzer." Proceedings of the Twenty-Second Annual ACM Symposium on - Parallelism in Algorithms and Architectures (2010): 145–156. 41 | - Mytkoqicz, Todd, Amer Diwan, et al. "Producing Wrong Data Without Doing Anything Obviously Wrong." Proceedings of the 14th International Conference on - Architectural Support for Programming Lanugages and Operating Systems (2009): 265–276. 42 | - Berger, Emery, Kathryn McKinley, et al. "This resource may not render correctly in a screen reader.Hoard: A Scalable Memory Allocator for Multithreaded - Applications (PDF)." Proceedings of the 9th International Conference on Architectural Support for Programming Languages and Operating Systems (2000): - 117–128. 43 | - Kuszmaul, Bradley. "SuperMalloc: A Super Fast Multithreaded Malloc for 64-bit Machines." Proceedings of the 2015 International Symposium on Memory - Management (2015): 41–55. 44 | - Schardl, Tao, William Moses, and Charles Leiserson. "Tapir: Embedding Fork-Join Parallelism into LLVM's Intermediate Representation." Proceedings of the - 22nd ACM SIGPLAN Symposium on Principles and Practice of Parallel Programming (2017): 249–265. 45 | - Frigo, Matteo, Charles Leiserson, et al. "Cache-Oblivious Algorithms." ACM Transactions on Algorithms (TALG) 8, no. 1 (2012): article no. 4. 46 | - Demaine, Erik. "Cache-Oblivious Algorithms and Data Structures" in Lecture Notes from the EEF Summer School on Massive Data Sets, BRICS (2002). 47 | - Leiserson, Charles. "A Simple Deterministic Algorithm for Guaranteeing the Forward Progress of Transactions." Information Systems 57 (2016): 69–74. 48 | --------------------------------------------------------------------------------