Skip to content

Commit

Permalink
add support for linux
Browse files Browse the repository at this point in the history
  • Loading branch information
polarker committed Dec 20, 2016
1 parent 9401a8a commit 07ac9df
Show file tree
Hide file tree
Showing 16 changed files with 88 additions and 50 deletions.
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
cmake_minimum_required(VERSION 3.0)

set(CMAKE_CXX_FLAGS "-std=c++11 -Wall -O3")
if (UNIX)
set(CMAKE_CXX_FLAGS "-std=c++11 -Wall -O3")
else()
error("only linux and macos are supported for the moment")
endif()

add_subdirectory(src)
add_subdirectory(example)
Expand Down
8 changes: 7 additions & 1 deletion example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,11 @@ file(GLOB sources ./*.cc)
foreach(source ${sources})
get_filename_component(source_we ${source} NAME_WE)
add_executable(${source_we} ${source})
target_link_libraries(${source_we} galois "-lz -framework Accelerate")
if (APPLE)
target_link_libraries(${source_we} galois "-lz -framework Accelerate")
elseif (UNIX)
target_link_libraries(${source_we} galois "-lz -lopenblas")
else()
error("only linux and macos are supported for the moment")
endif()
endforeach(source)
1 change: 1 addition & 0 deletions include/galois/gfilters/base_net.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <vector>
#include <set>
#include <map>
#include <algorithm>

using namespace std;

Expand Down
2 changes: 2 additions & 0 deletions include/galois/narray.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include "galois/utils.h"
#include <random>
#include <memory>
#include <iostream>
#include <vector>

using namespace std;
Expand Down
15 changes: 11 additions & 4 deletions include/galois/narray_functors.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
#ifndef _GALOIS_NARRAY_FUNCTORS_H_
#define _GALOIS_NARRAY_FUNCTORS_H_

#include <cassert>
#include <vector>
#if defined __APPLE__ && __MACH__
#include <Accelerate/Accelerate.h>
#elif defined __linux__
#include <cblas.h>
#else
// system not supported
#endif

namespace gs
{
Expand Down Expand Up @@ -196,8 +203,8 @@ namespace gs
}
}

inline void _GEMM(const enum CBLAS_ORDER _order,
const enum CBLAS_TRANSPOSE _tranA, const enum CBLAS_TRANSPOSE _tranB,
inline void _GEMM(const CBLAS_ORDER _order,
const CBLAS_TRANSPOSE _tranA, const CBLAS_TRANSPOSE _tranB,
const int _M, const int _N, const int _K,
const float _alpha,
const float *_A, const int _lda,
Expand All @@ -207,8 +214,8 @@ namespace gs
cblas_sgemm(_order, _tranA, _tranB, _M, _N, _K, _alpha, _A, _lda, _B, _ldb, _beta, _C, _ldc);
}

inline void _GEMM(const enum CBLAS_ORDER _order,
const enum CBLAS_TRANSPOSE _tranA, const enum CBLAS_TRANSPOSE _tranB,
inline void _GEMM(const CBLAS_ORDER _order,
const CBLAS_TRANSPOSE _tranA, const CBLAS_TRANSPOSE _tranB,
const int _M, const int _N, const int _K,
const double _alpha,
const double *_A, const int _lda,
Expand Down
4 changes: 2 additions & 2 deletions include/galois/optimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ namespace gs
void compile(vector<SP_NArray<T>> params, vector<SP_NArray<T>> grads) override {
CHECK(this->params.empty() && this->grads.empty(), "params and grads should not be set before");
CHECK(params.size() == grads.size(), "params and grads should have equal size");
for (int i = 0; i < params.size(); i++) {
for (size_t i = 0; i < params.size(); i++) {
CHECK(params[i]->get_dims() == grads[i]->get_dims(), "param and grad should have the same dimensions");
}
this->params.insert(this->params.end(), params.begin(), params.end());
this->grads.insert(this->grads.end(), grads.begin(), grads.end());
}

void update() override {
for (int i = 0; i < this->params.size(); i++) {
for (size_t i = 0; i < this->params.size(); i++) {
auto param = this->params[i];
auto grad = this->grads[i];
CHECK(!param->opaque(), "param should not be opaque");
Expand Down
1 change: 1 addition & 0 deletions include/galois/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <algorithm>

#define CHECK(A, M, ...) if(!(A)) { fprintf(stderr, "[ERROR] %s:%d: " M "\n", __FILE__, __LINE__, ##__VA_ARGS__); exit(EXIT_FAILURE); }

Expand Down
2 changes: 1 addition & 1 deletion src/gfilters/path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace gs
CHECK(links.size() == inner_signals.size()+1, "The number of filters and inner signals does not match");
auto in_signal = in_signals[0];
auto out_signal = out_signals[0];
for (int i = 0; i < links.size(); i++) {
for (size_t i = 0; i < links.size(); i++) {
SP_Signal<T> in = nullptr;
SP_Signal<T> out = nullptr;
if (i == 0) {
Expand Down
20 changes: 11 additions & 9 deletions src/models/bi_seq_encoder_decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "galois/gfilters/path.h"
#include "galois/filters.h"

#include <chrono>

namespace gs
{

Expand All @@ -25,22 +27,22 @@ namespace gs
h2hraw.push_back(make_shared<Linear<T>>(hsize, hsize));
}
auto x2hraw = vector<SP_Filter<T>>();
for (int i = 0; i < hidden_sizes.size(); i++) {
for (size_t i = 0; i < hidden_sizes.size(); i++) {
if (i == 0) {
x2hraw.push_back(make_shared<Embedding<T>>(input_size, hidden_sizes[i]));
} else {
x2hraw.push_back(make_shared<Linear<T>>(hidden_sizes[i-1], hidden_sizes[i]));
}
}
for (int i = 0; i < max_len; i++) {
for (int j = 0; j < hidden_sizes.size(); j++) {
for (size_t j = 0; j < hidden_sizes.size(); j++) {
string hraw = bi_seq_generate_id("hraw", i, j);
string left_h = bi_seq_generate_id("h", i-1, j);
if (i > 0) {
BaseNet<T>::add_link(left_h, hraw, h2hraw[j]->share());
}
}
for (int j = 0; j < hidden_sizes.size(); j++) {
for (size_t j = 0; j < hidden_sizes.size(); j++) {
string hraw = bi_seq_generate_id("hraw", i, j);
string down_h;
if (j == 0) {
Expand All @@ -58,7 +60,7 @@ namespace gs
for (int i = 0; i < max_len; i++) {
x_ids.push_back(bi_seq_generate_id("x", i));
}
for (int j = 0; j < hidden_sizes.size(); j++) {
for (size_t j = 0; j < hidden_sizes.size(); j++) {
y_ids.push_back(bi_seq_generate_id("h", max_len-1, j));
}
this->add_input_ids(x_ids);
Expand Down Expand Up @@ -91,7 +93,7 @@ namespace gs
h2hraw.push_back(make_shared<Linear<T>>(hsize, hsize));
}
auto x2hraw = vector<SP_Filter<T>>();
for (int i = 0; i < hidden_sizes.size(); i++) {
for (size_t i = 0; i < hidden_sizes.size(); i++) {
if (i == 0) {
x2hraw.push_back(make_shared<Embedding<T>>(input_size, hidden_sizes[i]));
} else {
Expand All @@ -101,12 +103,12 @@ namespace gs
auto h2yraw = make_shared<Linear<T>>(hidden_sizes.back(), input_size);

for (int i = 0; i < max_len; i++) {
for (int j = 0; j < hidden_sizes.size(); j++) {
for (size_t j = 0; j < hidden_sizes.size(); j++) {
string hraw = bi_seq_generate_id("hraw", i, j);
string left_h = bi_seq_generate_id("h", i-1, j);
BaseNet<T>::add_link(left_h, hraw, h2hraw[j]->share());
}
for (int j = 0; j < hidden_sizes.size(); j++) {
for (size_t j = 0; j < hidden_sizes.size(); j++) {
string hraw = bi_seq_generate_id("hraw", i, j);
string down_h;
if (j == 0) {
Expand All @@ -131,7 +133,7 @@ namespace gs
x_ids.push_back(bi_seq_generate_id("x", i));
y_ids.push_back(bi_seq_generate_id("y", i));
}
for (int i = 0; i < hidden_sizes.size(); i++) {
for (size_t i = 0; i < hidden_sizes.size(); i++) {
x_ids.push_back(bi_seq_generate_id("h", -1, i));
}
this->add_input_ids(x_ids);
Expand Down Expand Up @@ -196,7 +198,7 @@ namespace gs
y_ids_one2one.push_back(bi_seq_generate_id("y_one2one", i));
y_ids_another2one.push_back(bi_seq_generate_id("y_another2one", i));
}
for (int j = 0; j < hidden_sizes.size(); j++) {
for (size_t j = 0; j < hidden_sizes.size(); j++) {
h_ids_one.push_back(bi_seq_generate_id("h_one", max_len_one-1, j));
h_ids_another.push_back(bi_seq_generate_id("h_another", max_len_another-1, j));
}
Expand Down
4 changes: 3 additions & 1 deletion src/models/mlp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "galois/narray_functors.h"
#include "galois/models/mlp.h"

#include <chrono>

using namespace std;

namespace gs
Expand Down Expand Up @@ -37,7 +39,7 @@ namespace gs
auto tmp_params = pfilter->get_params();
auto tmp_grads = pfilter->get_grads();
CHECK(tmp_params.size() == tmp_grads.size(), "numbers of params and grads should be equal");
for (int i = 0; i < tmp_params.size(); i++) {
for (size_t i = 0; i < tmp_params.size(); i++) {
auto param = tmp_params[i];
auto grad = tmp_grads[i];
CHECK(param->get_dims() == grad->get_dims(), "param and grad should have the same dimensions");
Expand Down
18 changes: 10 additions & 8 deletions src/models/model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "galois/narray_functors.h"
#include "galois/models/model.h"

#include <chrono>

namespace gs
{

Expand Down Expand Up @@ -58,7 +60,7 @@ namespace gs
CHECK(input_ids.empty(), "input_ids should not be set before");
net.add_input_ids(ids);
input_ids = ids;
for (int i = 0; i < input_ids.size(); i++) {
for (size_t i = 0; i < input_ids.size(); i++) {
input_signals.push_back(make_shared<Signal<T>>(InputSignal));
}
}
Expand All @@ -78,7 +80,7 @@ namespace gs
CHECK(output_ids.empty(), "output_ids should not be set before");
net.add_output_ids(ids);
output_ids = ids;
for (int j = 0; j < output_ids.size(); j++) {
for (size_t j = 0; j < output_ids.size(); j++) {
output_signals.push_back(make_shared<Signal<T>>(OutputSignal));
}
}
Expand All @@ -97,7 +99,7 @@ namespace gs
auto tmp_params = pfilter->get_params();
auto tmp_grads = pfilter->get_grads();
CHECK(tmp_params.size() == tmp_grads.size(), "numbers of params and grads should be equal");
for (int i = 0; i < tmp_params.size(); i++) {
for (size_t i = 0; i < tmp_params.size(); i++) {
auto param = tmp_params[i];
auto grad = tmp_grads[i];
CHECK(param->get_dims() == grad->get_dims(), "param and grad should have the same dimensions");
Expand Down Expand Up @@ -204,11 +206,11 @@ namespace gs
}

net.reopaque();
for (int i = 0; i < input_signals.size(); i++) {
for (size_t i = 0; i < input_signals.size(); i++) {
input_signals[i]->reopaque();
input_signals[i]->get_data()->copy_from(batch_ids, train_data[i]);
}
for (int i = 0; i < output_signals.size(); i++) {
for (size_t i = 0; i < output_signals.size(); i++) {
output_signals[i]->reopaque();
output_signals[i]->get_target()->copy_from(batch_ids, train_target[i]);
}
Expand Down Expand Up @@ -242,17 +244,17 @@ namespace gs
}

net.reopaque();
for (int i = 0; i < input_signals.size(); i++) {
for (size_t i = 0; i < input_signals.size(); i++) {
input_signals[i]->reopaque();
input_signals[i]->get_data()->copy_from(batch_ids, test_data[i]);
}
for (int i = 0; i < output_signals.size(); i++) {
for (size_t i = 0; i < output_signals.size(); i++) {
output_signals[i]->reopaque();
output_signals[i]->get_target()->copy_from(batch_ids, test_target[i]);
}
net.forward();

for (int i = 0; i < output_signals.size(); i++) {
for (size_t i = 0; i < output_signals.size(); i++) {
correctness += compute_correctness(output_signals[i]);
}
}
Expand Down
18 changes: 10 additions & 8 deletions src/models/ordered_model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "galois/narray_functors.h"
#include "galois/models/ordered_model.h"

#include <chrono>

namespace gs
{

Expand Down Expand Up @@ -57,7 +59,7 @@ namespace gs
void OrderedModel<T>::add_input_ids(const vector<string> ids) {
net.add_input_ids(ids);
input_ids.insert(input_ids.end(), ids.begin(), ids.end());
for (int i = 0; i < input_ids.size(); i++) {
for (size_t i = 0; i < input_ids.size(); i++) {
input_signals.push_back(make_shared<Signal<T>>(InputSignal));
}
}
Expand All @@ -76,7 +78,7 @@ namespace gs
void OrderedModel<T>::add_output_ids(const vector<string> ids) {
net.add_output_ids(ids);
output_ids.insert(output_ids.end(), ids.begin(), ids.end());
for (int j = 0; j < output_ids.size(); j++) {
for (size_t j = 0; j < output_ids.size(); j++) {
output_signals.push_back(make_shared<Signal<T>>(OutputSignal));
}
}
Expand All @@ -95,7 +97,7 @@ namespace gs
auto tmp_params = pfilter->get_params();
auto tmp_grads = pfilter->get_grads();
CHECK(tmp_params.size() == tmp_grads.size(), "numbers of params and grads should be equal");
for (int i = 0; i < tmp_params.size(); i++) {
for (size_t i = 0; i < tmp_params.size(); i++) {
auto param = tmp_params[i];
auto grad = tmp_grads[i];
CHECK(param->get_dims() == grad->get_dims(), "param and grad should have the same dimensions");
Expand Down Expand Up @@ -202,11 +204,11 @@ namespace gs
}

net.reopaque();
for (int i = 0; i < input_signals.size(); i++) {
for (size_t i = 0; i < input_signals.size(); i++) {
input_signals[i]->reopaque();
input_signals[i]->get_data()->copy_from(batch_ids, train_data[i]);
}
for (int i = 0; i < output_signals.size(); i++) {
for (size_t i = 0; i < output_signals.size(); i++) {
output_signals[i]->reopaque();
output_signals[i]->get_target()->copy_from(batch_ids, train_target[i]);
}
Expand Down Expand Up @@ -240,17 +242,17 @@ namespace gs
}

net.reopaque();
for (int i = 0; i < input_signals.size(); i++) {
for (size_t i = 0; i < input_signals.size(); i++) {
input_signals[i]->reopaque();
input_signals[i]->get_data()->copy_from(batch_ids, test_data[i]);
}
for (int i = 0; i < output_signals.size(); i++) {
for (size_t i = 0; i < output_signals.size(); i++) {
output_signals[i]->reopaque();
output_signals[i]->get_target()->copy_from(batch_ids, test_target[i]);
}
net.forward();

for (int i = 0; i < output_signals.size(); i++) {
for (size_t i = 0; i < output_signals.size(); i++) {
correctness += compute_correctness(output_signals[i]);
}
}
Expand Down
Loading

0 comments on commit 07ac9df

Please sign in to comment.