From 07ac9df46b0e74377a99cb5cbee75cb9fb701209 Mon Sep 17 00:00:00 2001 From: Cheng Wang Date: Tue, 20 Dec 2016 19:49:23 +0100 Subject: [PATCH] add support for linux --- CMakeLists.txt | 6 +++++- example/CMakeLists.txt | 8 +++++++- include/galois/gfilters/base_net.h | 1 + include/galois/narray.h | 2 ++ include/galois/narray_functors.h | 15 +++++++++++---- include/galois/optimizer.h | 4 ++-- include/galois/utils.h | 1 + src/gfilters/path.cc | 2 +- src/models/bi_seq_encoder_decoder.cc | 20 +++++++++++--------- src/models/mlp.cc | 4 +++- src/models/model.cc | 18 ++++++++++-------- src/models/ordered_model.cc | 18 ++++++++++-------- src/models/rnn.cc | 10 ++++++---- src/models/seq_encoder_decoder.cc | 16 +++++++++------- src/narray.cc | 5 ++--- test/CMakeLists.txt | 8 +++++++- 16 files changed, 88 insertions(+), 50 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 58b5646..b8700a1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index ed47f82..66ba734 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -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) diff --git a/include/galois/gfilters/base_net.h b/include/galois/gfilters/base_net.h index 048e5d5..82ec6ef 100644 --- a/include/galois/gfilters/base_net.h +++ b/include/galois/gfilters/base_net.h @@ -6,6 +6,7 @@ #include #include #include +#include using namespace std; diff --git a/include/galois/narray.h b/include/galois/narray.h index a206316..57d8aa6 100644 --- a/include/galois/narray.h +++ b/include/galois/narray.h @@ -3,6 +3,8 @@ #include "galois/utils.h" #include +#include +#include #include using namespace std; diff --git a/include/galois/narray_functors.h b/include/galois/narray_functors.h index a01e6e9..ab7620e 100644 --- a/include/galois/narray_functors.h +++ b/include/galois/narray_functors.h @@ -1,8 +1,15 @@ #ifndef _GALOIS_NARRAY_FUNCTORS_H_ #define _GALOIS_NARRAY_FUNCTORS_H_ +#include #include +#if defined __APPLE__ && __MACH__ #include +#elif defined __linux__ +#include +#else +// system not supported +#endif namespace gs { @@ -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, @@ -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, diff --git a/include/galois/optimizer.h b/include/galois/optimizer.h index 12220aa..8ffa90b 100644 --- a/include/galois/optimizer.h +++ b/include/galois/optimizer.h @@ -31,7 +31,7 @@ namespace gs void compile(vector> params, vector> 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()); @@ -39,7 +39,7 @@ namespace gs } 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"); diff --git a/include/galois/utils.h b/include/galois/utils.h index c664fb1..8078d08 100644 --- a/include/galois/utils.h +++ b/include/galois/utils.h @@ -4,6 +4,7 @@ #include #include #include +#include #define CHECK(A, M, ...) if(!(A)) { fprintf(stderr, "[ERROR] %s:%d: " M "\n", __FILE__, __LINE__, ##__VA_ARGS__); exit(EXIT_FAILURE); } diff --git a/src/gfilters/path.cc b/src/gfilters/path.cc index c9f5601..9184414 100644 --- a/src/gfilters/path.cc +++ b/src/gfilters/path.cc @@ -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 in = nullptr; SP_Signal out = nullptr; if (i == 0) { diff --git a/src/models/bi_seq_encoder_decoder.cc b/src/models/bi_seq_encoder_decoder.cc index 80a5e74..2048bf8 100644 --- a/src/models/bi_seq_encoder_decoder.cc +++ b/src/models/bi_seq_encoder_decoder.cc @@ -2,6 +2,8 @@ #include "galois/gfilters/path.h" #include "galois/filters.h" +#include + namespace gs { @@ -25,7 +27,7 @@ namespace gs h2hraw.push_back(make_shared>(hsize, hsize)); } auto x2hraw = vector>(); - 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>(input_size, hidden_sizes[i])); } else { @@ -33,14 +35,14 @@ namespace gs } } 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::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) { @@ -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); @@ -91,7 +93,7 @@ namespace gs h2hraw.push_back(make_shared>(hsize, hsize)); } auto x2hraw = vector>(); - 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>(input_size, hidden_sizes[i])); } else { @@ -101,12 +103,12 @@ namespace gs auto h2yraw = make_shared>(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::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) { @@ -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); @@ -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)); } diff --git a/src/models/mlp.cc b/src/models/mlp.cc index 2eee16e..dcc4c89 100644 --- a/src/models/mlp.cc +++ b/src/models/mlp.cc @@ -2,6 +2,8 @@ #include "galois/narray_functors.h" #include "galois/models/mlp.h" +#include + using namespace std; namespace gs @@ -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"); diff --git a/src/models/model.cc b/src/models/model.cc index a2d0a52..427d4c6 100644 --- a/src/models/model.cc +++ b/src/models/model.cc @@ -2,6 +2,8 @@ #include "galois/narray_functors.h" #include "galois/models/model.h" +#include + namespace gs { @@ -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>(InputSignal)); } } @@ -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>(OutputSignal)); } } @@ -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"); @@ -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]); } @@ -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]); } } diff --git a/src/models/ordered_model.cc b/src/models/ordered_model.cc index 716d466..7e61f8e 100644 --- a/src/models/ordered_model.cc +++ b/src/models/ordered_model.cc @@ -2,6 +2,8 @@ #include "galois/narray_functors.h" #include "galois/models/ordered_model.h" +#include + namespace gs { @@ -57,7 +59,7 @@ namespace gs void OrderedModel::add_input_ids(const vector 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>(InputSignal)); } } @@ -76,7 +78,7 @@ namespace gs void OrderedModel::add_output_ids(const vector 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>(OutputSignal)); } } @@ -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"); @@ -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]); } @@ -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]); } } diff --git a/src/models/rnn.cc b/src/models/rnn.cc index 7062180..42de875 100644 --- a/src/models/rnn.cc +++ b/src/models/rnn.cc @@ -2,6 +2,8 @@ #include "galois/gfilters/path.h" #include "galois/filters.h" +#include + namespace gs { @@ -34,7 +36,7 @@ namespace gs h2hraw.push_back(make_shared>(hsize, hsize)); } auto x2hraw = vector>(); - for (int i = 0; i < hidden_sizes.size(); i++) { + for (size_t i = 0; i < hidden_sizes.size(); i++) { if (i == 0) { if (use_embedding) { x2hraw.push_back(make_shared>(input_size, hidden_sizes[i])); @@ -47,7 +49,7 @@ namespace gs } auto h2yraw = make_shared>(hidden_sizes.back(), output_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 = generate_id("hraw", i, j); string left_h = generate_id("h", i-1, j); string down_h; @@ -110,11 +112,11 @@ namespace gs template T RNN::train_one_batch(const int start_from, bool update) { this->net.reopaque(); - for (int i = 0; i < this->input_signals.size(); i++) { + for (size_t i = 0; i < this->input_signals.size(); i++) { this->input_signals[i]->reopaque(); this->input_signals[i]->get_data()->copy_from(start_from+i, this->batch_size, train_X); } - for (int i = 0; i < this->output_signals.size(); i++) { + for (size_t i = 0; i < this->output_signals.size(); i++) { this->output_signals[i]->reopaque(); this->output_signals[i]->get_target()->copy_from(start_from+i, this->batch_size, train_Y); } diff --git a/src/models/seq_encoder_decoder.cc b/src/models/seq_encoder_decoder.cc index 04afce5..ff4ac48 100644 --- a/src/models/seq_encoder_decoder.cc +++ b/src/models/seq_encoder_decoder.cc @@ -2,6 +2,8 @@ #include "galois/gfilters/path.h" #include "galois/filters.h" +#include + namespace gs { @@ -38,7 +40,7 @@ namespace gs h2hraw_decoder.push_back(make_shared>(hsize, hsize)); } auto x2hraw_encoder = vector>(); - for (int i = 0; i < hidden_sizes.size(); i++) { + for (size_t i = 0; i < hidden_sizes.size(); i++) { if (i == 0) { x2hraw_encoder.push_back(make_shared>(input_size, hidden_sizes[i])); } else { @@ -46,7 +48,7 @@ namespace gs } } auto x2hraw_decoder = vector>(); - for (int i = 0; i < hidden_sizes.size(); i++) { + for (size_t i = 0; i < hidden_sizes.size(); i++) { if (i == 0) { x2hraw_decoder.push_back(make_shared>(output_size, hidden_sizes[i])); } else { @@ -56,14 +58,14 @@ namespace gs auto h2yraw_decoder = make_shared>(hidden_sizes.back(), output_size); for (int i = 0; i < max_len_encoder; i++) { - for (int j = 0; j < hidden_sizes.size(); j++) { + for (size_t j = 0; j < hidden_sizes.size(); j++) { string hraw = seq_generate_id("hraw_encoder", i, j); string left_h = seq_generate_id("h_encoder", i-1, j); if (i > 0) { this->add_link(left_h, hraw, h2hraw_encoder[j]->share()); } } - for (int j = 0; j < hidden_sizes.size(); j++) { + for (size_t j = 0; j < hidden_sizes.size(); j++) { string hraw = seq_generate_id("hraw_encoder", i, j); string down_h; if (j == 0) { @@ -77,7 +79,7 @@ namespace gs } } for (int i = 0; i < max_len_decoder; i++) { - for (int j = 0; j < hidden_sizes.size(); j++) { + for (size_t j = 0; j < hidden_sizes.size(); j++) { string hraw = seq_generate_id("hraw_decoder", i, j); string left_h; if (i == 0) { @@ -87,7 +89,7 @@ namespace gs } this->add_link(left_h, hraw, h2hraw_decoder[j]->share()); } - for (int j = 0; j < hidden_sizes.size(); j++) { + for (size_t j = 0; j < hidden_sizes.size(); j++) { string hraw = seq_generate_id("hraw_decoder", i, j); string down_h; if (j == 0) { @@ -176,7 +178,7 @@ namespace gs input_data->copy_from(prev_output_data); } int num2 = num1+i*(hidden_sizes.size()*3 + 2); - for (int j = num2; j < hidden_sizes.size()*3 + 2; j++) { + for (size_t j = num2; j < hidden_sizes.size()*3 + 2; j++) { this->net.forward(i); } } diff --git a/src/narray.cc b/src/narray.cc index 8628fd3..12ac529 100644 --- a/src/narray.cc +++ b/src/narray.cc @@ -86,7 +86,7 @@ namespace gs auto dataset_dims = dataset->get_dims(); CHECK(idx0s.size() == this->dims[0], "first dimension should be equal to batch size"); CHECK(dataset_dims.size() == this->dims.size()+1, "number of dimensions should be equal"); - for (int i = 2; i < this->dims.size(); i++) { + for (size_t i = 2; i < this->dims.size(); i++) { CHECK(dataset_dims[i] == this->dims[i-1], "dimensions should be equal"); } CHECK(idx1 >= 0 && idx1 < this->dims[1], "invalid index"); @@ -109,7 +109,7 @@ namespace gs auto dataset_dims = dataset->get_dims(); CHECK(copy_size == this->dims[0], "the size of copy should be equal to batch size"); CHECK(dataset_dims.size() == this->dims.size(), "number of dimensions should be equal"); - for (int i = 1; i < this->dims.size(); i++) { + for (size_t i = 1; i < this->dims.size(); i++) { CHECK(dataset_dims[i] == this->dims[i], "dimensions should be equal"); } CHECK(start_from >= 0 && start_from+copy_size-1 < dataset_dims[0], "offset is not valid"); @@ -156,7 +156,6 @@ namespace gs } } - template class NArray; template class NArray; template class NArray; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 36b82c0..0d67ed9 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -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)