Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
print error log
Browse files Browse the repository at this point in the history
  • Loading branch information
nihui committed Mar 29, 2019
1 parent 1adbaff commit 6a5b2d3
Showing 1 changed file with 36 additions and 23 deletions.
59 changes: 36 additions & 23 deletions cpp-package/example/test_kvstore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@

using namespace mxnet::cpp;

static int test_single_key() {
int ret = 0;

static bool test_single_key() {
std::string key = "singlekeytest";

NDArray result(Shape(4), Context::cpu());
Expand All @@ -38,10 +36,12 @@ static int test_single_key() {

// compare
for (size_t j=0; j < result.Size(); j++) {
ret += (result.GetData()[j] == data.GetData()[j]) ? 0 : 1;
if (result.GetData()[j] != data.GetData()[j]) {
LG << "Error: wrong initialized data in singlekeytest, expect "
<< data.GetData()[j] << " got " << result.GetData()[j];
return false;
}
}
if (ret != 0)
return ret;

// push gradient
NDArray grad({0.1f, -2.f, -4.4f, 0.f}, Shape(4), Context::cpu());
Expand All @@ -54,15 +54,17 @@ static int test_single_key() {

// compare
for (size_t j=0; j < result.Size(); j++) {
ret += (result.GetData()[j] == grad.GetData()[j]) ? 0 : 1;
if (result.GetData()[j] == grad.GetData()[j]) {
LG << "Error: wrong gradient data in singlekeytest, expect "
<< grad.GetData()[j] << " got " << result.GetData()[j];
return false;
}
}

return ret;
return true;
}

static int test_multiple_key() {
int ret = 0;

static bool test_multiple_key() {
std::vector<std::string> keys(2);
keys[0] = "multikeytest-0";
keys[1] = "multikeytest-1";
Expand All @@ -72,10 +74,10 @@ static int test_multiple_key() {
results[1] = NDArray(Shape(4), Context::cpu());

// initialize data
std::vector<NDArray> datas(2);
datas[0] = NDArray({0.f, 2.f, -3.12f, 4.f}, Shape(4), Context::cpu());
datas[1] = NDArray({0.8f, -2.f, 6.6f, 77.f}, Shape(4), Context::cpu());
KVStore::Init(keys, datas);
std::vector<NDArray> data(2);
data[0] = NDArray({0.f, 2.f, -3.12f, 4.f}, Shape(4), Context::cpu());
data[1] = NDArray({0.8f, -2.f, 6.6f, 77.f}, Shape(4), Context::cpu());
KVStore::Init(keys, data);
NDArray::WaitAll();

// retrieve result
Expand All @@ -85,11 +87,13 @@ static int test_multiple_key() {
// compare
for (size_t i=0; i < results.size(); i++) {
for (size_t j=0; j < results[i].Size(); j++) {
ret += (results[i].GetData()[j] == datas[i].GetData()[j]) ? 0 : 1;
if (results[i].GetData()[j] == data[i].GetData()[j]) {
LG << "Error: wrong initialized data in multikeytest, expect "
<< data[i].GetData()[j] << " got " << results[i].GetData()[j];
return false;
}
}
}
if (ret != 0)
return ret;

// push gradient, reduce for the second
std::vector<std::string> push_keys(3);
Expand All @@ -110,22 +114,31 @@ static int test_multiple_key() {

// compare the first
for (size_t j=0; j < results[0].Size(); j++) {
ret += (results[0].GetData()[j] == grads[0].GetData()[j]) ? 0 : 1;
if (results[0].GetData()[j] == grads[0].GetData()[j]) {
LG << "Error: wrong gradient data, expect " << grads[0].GetData()[j]
<< " got " << result[0].GetData()[j];
return false;
}
}

// compare the second
for (size_t j=0; j < results[1].Size(); j++) {
ret += (results[1].GetData()[j] == (grads[1].GetData()[j] + grads[2].GetData()[j])) ? 0 : 1;
if (results[1].GetData()[j] == (grads[1].GetData()[j] + grads[2].GetData()[j])) {
LG << "Error: wrong reduced gradient data, expect "
<< (grads[1].GetData()[j] + grads[2].GetData()[j])
<< " got " << result[1].GetData()[j];
return false;
}
}

return ret;
return true;
}

int main(int argc, char** argv) {
KVStore::SetType("local");

int ret1 = test_single_key();
int ret2 = test_multiple_key();
bool ret1 = test_single_key();
bool ret2 = test_multiple_key();

MXNotifyShutdown();
return ret1 + ret2;
Expand Down

0 comments on commit 6a5b2d3

Please sign in to comment.