The projects are part of your design project worth 2 credit points. As such they run in parallel to the actual course. So be aware that the due date for project and homework might be very close to each other! Start early and do not procrastinate.
In this project, we hope you can use all knowledge about computer architecture that your have learnt in this course to optimize a very simple, yet slow program.
Make sure you read through the entire specification before starting the project.
You will be using gitlab to collaborate with your group partner. Autolab will use the files from gitlab. Make sure that you have access to gitlab. In the group CS110_Projects you should have access to your project 3 project. Also, in the group CS110, you should have access to the p3_framework.
The framework contains the following files:
If you wish to test on your own machine, please modify the ARCH variable in Makefile. You can search your CPU's model name to find out which microarchitecture it belongs to.
For example, suppose your CPU is i5-6300HQ, the specification page for it says it belongs to the Skylake microarchitecture, thus you should change ARCH to skylake in the Makefile.
For a list of supported microarchitectures in GCC, please refer to x86 Options. Note: some newer microarchitectures are not supported by old versions of GCC, you can either enable the corresponding SIMD instructions manually or upgrade GCC.
Given two arrays \(a\) and \(b\) of length \(n\) which contains positive integers less than or equal to 8. We assume that the array index starts at zero, calculate the \(c\) array given the following formula:
$$c_k=\sum_{i=0}^{n-k-1}{a_{i+k}b_i},\quad 0 \leq k < n$$The first line contains a positive integer \(n\). There are two positive integers in each of the next \(n\) lines representing the elements in the two arrays, i.e., the \((i+2)\)-th line contains \(a_i\), \(b_i\). For the given input, \(n \leq 2^{25}\).
5 3 1 2 4 1 1 2 4 1 4
\(n\) lines. The \((k+1)\)-th line contains one positive number which represents \(c_k\).
24 12 10 6 1
The problem is very simple, and your job is to optimize the naive \(O(n^2)\) algorithm.
In fact, there is a faster algorithm (see below) to solve this problem. We provide you with the faster algorithm so that you can get correct result on large inputs quickly. You are not allowed to optimize the naive algorithm though, as this is not the focus of Computer Architecture. You will receive no point if we find you do that.
There are some optimization flags that can be turned on in GCC. However, we wish you to do the optimization on your own, instead of relying on the compiler to do it for you. You will receive 0 points if you try to turn on any other optimization flags except for -O2 specified in Makefile.
The first and the easiest approach is to use multithreading to optimize this algorithm. There are hardly any data dependencies so you do not need to consider much about synchronization. However, the computation workload for each \(c_k\) is imbalanced, you should think about how to balance it.
This algorithm is also a good candidate for SIMD instructions.
Loop unrolling works very well in combination with SIMD instructions for this algorithm, and you should think about it (Hint: there are two kinds of load: one require alignment and one do not).
For each computation of \(c_k\), we just stride across the data. If \(n\) is very large, this wastes cache lines and may slow down your program. One way to solve this is to reuse the cache line for next loop (e.g. \(c_{k+1}\)).
Your grade will be divided into two parts:
When your project is done, please submit all the files including the framework to your remote GitLab repo by running the following commands.
$ git commit -a $ git push origin master:master
Autolab will discard all other files except for calc.cpp, so please put all your code in that file.
Similar to previous projects, upload your autolab.txt to Autolab to submit your project.
The last time of your submission to the git repo will count towards your submission time - also with respect to slip days. So do not commit to this git after the due date, unless you want to use slip days or are OK with getting fewer points.
You have to work at this project as a team. We invite you to use all of the features of gitlab for your project, for example branches, issues, wiki, milestones, etc.
We require you to push very frequently to gitlab. In your commits we want to see how the code evolved. We do NOT want to see the working code suddenly appear - this will make us suspicious.
We also require that all group members do substantial contributions to the project. This also means that one group member should not finish the project all by himself, but distribute the work among all group members!
At the end of Project 3 we will interview all group members and discuss their contributions, to see if we need to modify the score for certain group members.
Actually, there is a faster algorithm to solve this problem.
We can do a simple transform on \(a\) and \(c\). Let \(a^{\prime}_k=a_{n-k-1}\), \(c^{\prime}_k=c_{n-k-1}\), the equation becomes
$$c^{\prime}_k=\sum_{i=0}^{k}{a^{\prime}_ib_{k-i}},\quad 0 \leq k < n$$This is the first half of polynomial multiplication, and we can use Fast Fourier Transform to solve it in \(O(n\log n)\) time.