Skip to content

Commit

Permalink
parallelize synapse generation using OpenMP
Browse files Browse the repository at this point in the history
- only tested on Debian Linux with a 16-thread amd64 CPU
- Windows may need DLLs to be shipped with the EXE
- using `-fopenmp` should be made optional via qmake somehow
  (without the flag, parallelization pragmas are ignored
  and it reverts to serial operation)
- assumes `status::update()` among other code is thread-safe,
  it seems to work on my machine without issues...
  • Loading branch information
claudeha authored and aeiouaeiouaeiouaeiouaeiouaeiou committed Sep 27, 2022
1 parent d4816e3 commit 12f3270
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
25 changes: 15 additions & 10 deletions brain/src/brain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <iostream>
#include <algorithm>
#include <atomic>
#include <sndfile.h>
#include <float.h>
#include <spiralcore/audio.h>
Expand Down Expand Up @@ -242,10 +243,12 @@ void brain::build_synapses_thresh(search_params &params, double thresh) {
m_average_error = calc_average_diff(params)*thresh;
double err = m_average_error*thresh;
u32 brain_size = m_blocks.size();
u32 outer_index = 0;
for (auto &i : m_blocks) {
std::atomic<u32> progress{0};
#pragma omp parallel for
for (u32 outer_index = 0; outer_index < brain_size; ++outer_index) {
auto &i = m_blocks[outer_index];
u32 index = 0;
status::update("building synapses %d%%",(int)(outer_index/(float)brain_size*100));
status::update("building synapses %d%%",(int)(progress/(float)brain_size*100));
for (auto &j : m_blocks) {
if (index!=outer_index) {
// collect connections that are under threshold in closeness
Expand All @@ -256,30 +259,32 @@ void brain::build_synapses_thresh(search_params &params, double thresh) {
}
++index;
}
++outer_index;
++progress;
}
}

void brain::build_synapses_fixed(search_params &params) {
//m_average_error = calc_average_diff(params)*thresh;
u32 brain_size = m_blocks.size();
u32 outer_index = 0;
u32 num_synapses = NUM_FIXED_SYNAPSES;
if (num_synapses>=m_blocks.size()) num_synapses=m_blocks.size()-1;

// need to stop the progress updates flooding osc
u32 update_period = 100;
u32 update_tick = 0;

for (auto &i:m_blocks) {
std::atomic<u32> update_tick{0};
std::atomic<u32> progress{0};
#pragma omp parallel for
for (u32 outer_index = 0; outer_index < brain_size; ++outer_index) {
auto &i = m_blocks[outer_index];
if (update_tick>update_period) {
status::update("building synapses %d%%",(int)(outer_index/(float)brain_size*100));
status::update("building synapses %d%%",(int)(progress/(float)brain_size*100));
update_tick=0;
}
update_tick++;

u32 index = 0;
vector<pair<u32,double>> collect;
collect.reserve(brain_size);

// collect comparisons to all other blocks
for (auto &j:m_blocks) {
Expand All @@ -304,7 +309,7 @@ void brain::build_synapses_fixed(search_params &params) {
i.get_synapse().push_back(collect[n].first);
}

++outer_index;
++progress;
}
status::update("Done: %d synapses grown for %d blocks",num_synapses*brain_size,brain_size);
}
Expand Down
4 changes: 2 additions & 2 deletions samplebrain.pro
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ SOURCES += app/MainWindow.cpp \
INCLUDEPATH += brain/src
INCLUDEPATH += /usr/local/include
INCLUDEPATH += /opt/local/include
LIBS += -L.. -L/usr/local/lib -L/opt/local/lib -lportaudio -lfftw3 -lsndfile -llo -ldl -lpthread -lm
LIBS += -L.. -L/usr/local/lib -L/opt/local/lib -lportaudio -lfftw3 -lsndfile -llo -ldl -lpthread -lm -fopenmp

QMAKE_CXXFLAGS += -O3 -march=core2 -Wall -Wno-unused -std=c++11
QMAKE_CXXFLAGS += -O3 -march=core2 -fopenmp -Wall -Wno-unused -std=c++11

# assets
RESOURCES = app/samplebrain.qrc
Expand Down

0 comments on commit 12f3270

Please sign in to comment.