Skip to content

Commit

Permalink
Adding initial files
Browse files Browse the repository at this point in the history
  • Loading branch information
tkralphs committed Aug 9, 2020
0 parents commit 9bc59db
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*~
*.o
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ted Ralphs <[email protected]>
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright 2020, Ted Ralphs

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

53 changes: 53 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
CXX ?= g++

BUILD_DIR ?= build
SRC_DIR ?= src

SRCS := $(shell find $(SRC_DIR) -name '*.cpp')
OBJS := $(SRCS:$(SRC_DIR)/%.cpp=$(BUILD_DIR)/%.o)
DEPS := $(OBJS:.o=.d)

INC_DIRS := $(shell find $(SRC_DIR) -type d)
INC_FLAGS := $(addprefix -I,$(INC_DIRS))

SUM_TEST ?= false

CPPFLAGS ?= $(INC_FLAGS) -O2 -pedantic-errors -Wall -Wextra -Werror -MMD -MP

mult: $(OBJS)
@echo "Linking: $@"
@$(CXX) $(OBJS) -o $@ $(LDFLAGS)

sum: $(OBJS)
@echo "Linking: $@"
@$(CXX) $(OBJS) -o $@ -DSUM $(LDFLAGS)

# c source
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c
@$(MKDIR_P) $(dir $@)
@echo "Compiling: $< -> $@"
@$(CXX) $(CPPFLAGS) -c $< -o $@

# c++ source
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.cpp
@$(MKDIR_P) $(dir $@)
@echo "Compiling: $< -> $@"
@$(CXX) $(CPPFLAGS) -c $< -o $@

.PHONY: test
sum-test: sum
python3 scripts/test.py sum

mult-test: mult
python3 scripts/test.py mult

.PHONY: clean
clean:
@echo "Deleting build directory"
@$(RM) -r $(BUILD_DIR)
@echo "Deleting binary"
@$(RM) $(BIN_NAME)

-include $(DEPS)

MKDIR_P ?= mkdir -p
76 changes: 76 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# CacheTest

This project is distributed in association with the [INFORMS Journal in
Computing](https://pubsonline.informs.org/journal/ijoc) under the [MIT License](LICENSE).

[![IJOC logo](https://INFORMSJoC.github.io/logo.png)](https://pubsonline.informs.org/journal/ijoc)

The software and data in this repository are associated with the paper [This is a Template](https://doi.org/10.1287/ijoc.2019.0934) by T. Ralphs.

[![Release](https://img.shields.io/github/v/release/INFORMSJoC/Template?sort=semver)](https://github.com/INFORMSJoC/Template/releases)

The goal of this software is to demonstrate the effect of cache optimization.

## Building

In Linux, to build the version that multiplies all elements of a vector by a
constant (used to obtain the results in [Figure 1](results/mult-test.png) in the
paper), stepping K elements at a time, execute the following commands.

```
make mult
```

Alternatively, to build the version that sums the elements of a vector (used
to obtain the results [Figure 2](results/sum-test.png) in the paper), stepping K
elements at a time, do the following.

```
make clean
make sum
```

Be sure to make clean before building a different version of the code.

## Results

Figure 1 in the paper shows the results of the multiplication test with different
values of K using `gcc` 7.5 on an Ubuntu Linux box.

![Figure 1](results/mult-test.png)

Figure 2 in the paper shows the results of the sum test with different
values of K using `gcc` 7.5 on an Ubuntu Linux box.

![Figure 1](results/sum-test.png)

## Replicating

To replicate the results in [Figure 1](results/mult-test), do either

```
make mult-test
```
or
```
python test.py mult
```
To replicate the results in [Figure 2](results/sum-test), do either

```
make sum-test
```
or
```
python test.py sum
```

## Ongoing Development

This code is being developed on an on-going basis at the author's
[Github site](https://github.com/tkralphs/JoCTemplate).

## Support

For support in using this software, submit an
[issue](https://github.com/tkralphs/JoCTemplate/issues/new).
Binary file added results/mult-test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added results/sum-test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions scripts/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@



###############################################################################
# Script for generating results from paper
###############################################################################

import subprocess
import matplotlib.pyplot as plt
import os, sys

sizes = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]
results = {i : [] for i in sizes}

for i in sizes:
print("Running with step size %d" % i)
c = subprocess.run([os.path.join(os.getcwd(), sys.argv[1]), "%d" % i,
"1000000000"], stdout = subprocess.PIPE)
print(c.stdout.decode('ascii'))
results[i].append(float(c.stdout.decode('ascii').split()[-1]))


plt.plot(sizes, list(results.values()))
#plt.legend()
plt.title("Update Every K-th Int")
plt.xlabel("K")
plt.ylabel("Time(ms)")
plt.xscale('log', basex=2)
plt.xticks(sizes, sizes)
plt.show()
48 changes: 48 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <iostream>
#include <vector>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

int main(int argc, char* argv[])
{
if (argc != 3){
cout << "Program takes two arguments, vector size and step size!" << endl;
exit(1);
}
int size_of_step = atoi(argv[1]);
int size_of_vector = atoi(argv[2]);

clock_t start = clock();

vector<double> myVector(size_of_vector);
srand((unsigned int)time(NULL));
for(int i=0; i != size_of_vector; ++i) {
myVector[i]= double( rand() ) / RAND_MAX;
}

clock_t timeElapsed = clock() - start;
double msElapsed = double(timeElapsed) / CLOCKS_PER_SEC;
//cout << "Memory Allocation:" << msElapsed << endl;

start = clock();

#ifdef SUM
double sum(0);
#endif
for (size_t i = 0; i < myVector.size(); i += size_of_step) {
#ifdef SUM
sum += myVector[i];
#else
myVector[i] *= 3;
#endif
}

timeElapsed = clock() - start;
msElapsed = double(timeElapsed) / CLOCKS_PER_SEC;
cout << "Running Time: " << msElapsed << endl;

return 0;
}

0 comments on commit 9bc59db

Please sign in to comment.