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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: C/C++ CI

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y build-essential libsdl2-dev libsdl2-image-dev g++
- name: make check
run: make check
21 changes: 19 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ all:$(BUILD_DIR)
$(BUILD_DIR):
mkdir -p $(BUILD_DIR)

TESTS = $(wildcard unit_tests*.cpp)
TESTS = $(wildcard tests*.cpp)
TESTS_TARGETS = $(TESTS:.cpp=)

unit: $(BUILD_DIR) $(TESTS_TARGETS)
Expand All @@ -18,4 +18,21 @@ unit: $(BUILD_DIR) $(TESTS_TARGETS)
g++ -g -Iheaders -o $(BUILD_DIR)/$@ $<

clean:
rm -rf $(BUILD_DIR)
rm -rf $(BUILD_DIR)

check:$(BUILD_DIR)
g++ -O3 -std=c++17 `sdl2-config --libs` -lSDL2_image -o $(BUILD_DIR)/basic_identity tests/basic_identity.cpp
./build/basic_identity
g++ -O3 -std=c++17 -fopenmp -DNUM_THREADS=2 `sdl2-config --libs` -lSDL2_image -o $(BUILD_DIR)/basic_identity tests/basic_identity.cpp
./build/basic_identity
g++ -O3 -std=c++17 `sdl2-config --libs` -lSDL2_image -o $(BUILD_DIR)/basic_no_restrictions tests/basic_no_restrictions.cpp
./build/basic_no_restrictions
g++ -O3 -std=c++17 -fopenmp -DNUM_THREADS=2 `sdl2-config --libs` -lSDL2_image -o $(BUILD_DIR)/basic_no_restrictions tests/basic_no_restrictions.cpp
./build/basic_no_restrictions
# removed from loop because it is unpredictable
# g++ -O3 -std=c++17 -fopenmp -DNUM_THREADS=2 `sdl2-config --libs` -lSDL2_image -o $(BUILD_DIR)/basic_random_adjacency tests/basic_random_adjacency.cpp
# ./build/basic_random_adjacency
g++ -O3 -std=c++17 `sdl2-config --libs` -lSDL2_image -o $(BUILD_DIR)/basic_smooth_noise tests/basic_smooth_noise.cpp
./build/basic_smooth_noise
g++ -O3 -std=c++17 -fopenmp -DNUM_THREADS=2 `sdl2-config --libs` -lSDL2_image -o $(BUILD_DIR)/basic_smooth_noise tests/basic_smooth_noise.cpp
./build/basic_smooth_noise
6 changes: 1 addition & 5 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
#include <cstdio>
#include <iostream>
#include <chrono>
using namespace std;

#include "src/common.h"
#include "src/structures.h"
#include "src/solver.h"
#include "src/argument_parser.h"
#include "src/timer.h"

#include "src/examples/patterns.h"
#include "src/examples/pipes.h"
#include "src/examples/pipes_colored.h"
#include "src/examples/patterns.h"

int main( int argc, char* argv[]){
ArgumentParser parser(argc, argv);
Expand Down
3 changes: 2 additions & 1 deletion src/common.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include <assert.h>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <omp.h>
using namespace std;
Expand All @@ -12,7 +13,7 @@ class Matrix3D{
int size;
type* values;
public:
Matrix3D(int sx, int sy, int sz): sx(sx), sy(sy), sz(sz),size(sx*sy*sz), values(static_cast<type*>(std::aligned_alloc(64, size * sizeof(type)))) {}
Matrix3D(int sx, int sy, int sz): sx(sx), sy(sy), sz(sz),size(sx*sy*sz), values(static_cast<type*>(aligned_alloc(64, size * sizeof(type)))) {}

int get_size(){return size;}
int get_sx(){return sx;}
Expand Down
15 changes: 13 additions & 2 deletions src/solver.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class WFCSolver{

// Solver settings
bool log_progress = false;
bool log_result = true;
bool log_result = false;
bool sucessful = false;
bool periodic = false;
int propagation_method = 0;
Expand Down Expand Up @@ -200,12 +200,19 @@ class WFCSolver{
int num_of_steps = 0;
int progress = 0;
sucessful = true;

int last_progress = -1;


// Main solver loop
while(collapse_path_length < total_size && !terminate){
num_of_steps++;
if(log_progress){
int progress = (100 * (collapse_path_length)) / total_size;
if(progress % 1 == 0 && progress != last_progress) {
last_progress = progress;
std::cout << "\rProgress: " << progress << "% (" << collapse_path_length << "/" << total_size << ")" << " setbacks: " << num_of_steps-collapse_path_length << std::flush;
}
}
// try to collapse another
entropy = data.least_entropy(next_x, next_y);
//cout << next_x << " " << next_y << " " << entropy << " " << collapse_path_length << endl;
Expand Down Expand Up @@ -263,6 +270,10 @@ class WFCSolver{
need_to_be_updated = data.need_to_be_updated();
}
}
if(log_progress){
std::cout << "\rProgress: " << "100% (" << collapse_path_length << "/" << total_size << ")" << " setbacks: " << num_of_steps-collapse_path_length << std::flush;
cout << "\n" << endl;
}
if(log_result){
cout << collapse_path_length << endl;
cout << data.total_updates << endl;
Expand Down
22 changes: 7 additions & 15 deletions unit_tests/basic_identity.cpp → tests/basic_identity.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
#include <cstdio>
#include <iostream>
#include <random>
#include <vector>
using namespace std;
#include "common.h"
#include "structures.h"
//#include "headers/converter.h"
#include "solver.h"
#include "visualizer.h"
#include "../src/solver.h"

int main( int argc, char* argv[]){
int sx = 10, sy = 10;
Expand All @@ -23,20 +14,21 @@ int main( int argc, char* argv[]){

WFCDataStructure data = WFCDataStructure(sx,sy,options,adjacency_rules);
WFCSolver solver = WFCSolver();
#ifdef _OPENMP
Matrix2D<int> result = solver.solve(data);
adjacency_matrix_x.print();
result.print();
#else
Matrix2D<int> result = solver.solve_sequential(data);
#endif
//adjacency_matrix_x.print();
//result.print();

// Unit test result check
int first_value = result.get(0);
for (int i = 0; i<sx*sy; i++) {
if(result.get(i) != first_value){
cout << "Unit test unsucessful:" << endl;
cout << "All values are not the same." << endl;
return 1;
}
}
cout << "Unit test sucessful" << endl;
return 0;
}

Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
#include <cstdio>
#include <iostream>
#include <random>
#include <vector>
using namespace std;
#include "common.h"
#include "structures.h"
//#include "headers/converter.h"
#include "solver.h"
#include "visualizer.h"
#include "../src/solver.h"


int main( int argc, char* argv[]){
int sx = 10, sy = 10;
Expand All @@ -29,19 +21,17 @@ int main( int argc, char* argv[]){

WFCDataStructure data = WFCDataStructure(sx,sy,options,adjacency_rules);
WFCSolver solver = WFCSolver();
#ifdef _OPENMP
Matrix2D<int> result = solver.solve(data);
//adjacency_matrix_x.print();
//adjacency_matrix_y.print();
//result.print();
#else
Matrix2D<int> result = solver.solve_sequential(data);
#endif

// Unit test result check

if(solver.sucessful){
cout << "Unit test sucessful:" << endl;
}else{
cout << "Unit test unsucessful:" << endl;
if(!solver.sucessful){
return 1;
}

return 0;
}

Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
#include <cstdio>
#include <iostream>
#include <random>
#include <vector>
using namespace std;
#include "common.h"
#include "structures.h"
//#include "headers/converter.h"
#include "solver.h"
#include "visualizer.h"
#include "../src/solver.h"

int main( int argc, char* argv[]){
int sx = 10, sy = 10;
Expand All @@ -34,13 +25,12 @@ int main( int argc, char* argv[]){

WFCDataStructure data = WFCDataStructure(sx,sy,options,adjacency_rules);
WFCSolver solver = WFCSolver();
#ifdef _OPENMP
Matrix2D<int> result = solver.solve(data);
adjacency_matrix_x.print();
adjacency_matrix_y.print();
result.print();

cout << "^ check unit test result manually" << endl;

#else
Matrix2D<int> result = solver.solve_sequential(data);
#endif
// Random adjacency => code is correct if this finishes
return 0;
}

36 changes: 12 additions & 24 deletions unit_tests/basic_smooth_noise.cpp → tests/basic_smooth_noise.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
#include <cstdio>
#include <iostream>
#include <random>
#include <vector>
using namespace std;
#include "common.h"
#include "structures.h"
//#include "headers/converter.h"
#include "solver.h"
#include "visualizer.h"
#include "../src/solver.h"

int main( int argc, char* argv[]){
int sx = 10, sy = 10;
Expand All @@ -29,27 +20,24 @@ int main( int argc, char* argv[]){

WFCDataStructure data = WFCDataStructure(sx,sy,options,adjacency_rules);
WFCSolver solver = WFCSolver();
#ifdef _OPENMP
Matrix2D<int> result = solver.solve(data);
adjacency_matrix_x.print();
result.print();
#else
Matrix2D<int> result = solver.solve_sequential(data);
#endif

// Unit test result check
// all differences must be exactly one
for (int x = 0; x < sx - 1; x++) {
for (int y = 0; y < sy - 1; y++) {
if(abs(result.get(x,y) - result.get(x+1,y)) != 1){
cout << "Unit test unsucessful:" << endl;
cout << "difference too big on " << x << " " << y << endl;
break;
for (int y = 0; y < sy - 1; y++) {
if(abs(result.get(x,y) - result.get(x+1,y)) != 1){
return 1;
}
if(abs(result.get(x,y) - result.get(x,y+1)) != 1){
return 1;
}
}
if(abs(result.get(x,y) - result.get(x,y+1)) != 1){
cout << "Unit test unsucessful:" << endl;
cout << "difference too big on " << x << " " << y << endl;
break;
}
}
}
cout << "Unit test sucessful" << endl;

return 0;
}
Expand Down