Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Add support for multithreaded training in the neural net example #2454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
Loading
from
Open
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
12 changes: 9 additions & 3 deletions 12 examples/machine_learning/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ project(ArrayFire-Example-Linear-Algebra
LANGUAGES CXX)

find_package(ArrayFire)
find_package(Threads)

add_definitions("-DASSETS_DIR=\"${ASSETS_DIR}\"")

Expand Down Expand Up @@ -41,7 +42,8 @@ if(ArrayFire_CPU_FOUND)

# Neural Network example
add_executable(neural_network_cpu neural_network.cpp)
target_link_libraries(neural_network_cpu ArrayFire::afcpu)
set_target_properties(neural_network_cpu PROPERTIES CXX_STANDARD 11)
target_link_libraries(neural_network_cpu ArrayFire::afcpu Threads::Threads)

# Preceptron example
add_executable(perceptron_cpu perceptron.cpp)
Expand Down Expand Up @@ -76,7 +78,9 @@ if(ArrayFire_CUDA_FOUND)
target_link_libraries(naive_bayes_cuda ArrayFire::afcuda)

add_executable(neural_network_cuda neural_network.cpp)
target_link_libraries(neural_network_cuda ArrayFire::afcuda)
set_target_properties(neural_network_cuda PROPERTIES CXX_STANDARD 11)
target_link_libraries(neural_network_cuda
ArrayFire::afcuda Threads::Threads)

add_executable(perceptron_cuda perceptron.cpp)
target_link_libraries(perceptron_cuda ArrayFire::afcuda)
Expand Down Expand Up @@ -108,7 +112,9 @@ if(ArrayFire_OpenCL_FOUND)
target_link_libraries(naive_bayes_opencl ArrayFire::afopencl)

add_executable(neural_network_opencl neural_network.cpp)
target_link_libraries(neural_network_opencl ArrayFire::afopencl)
set_target_properties(neural_network_opencl PROPERTIES CXX_STANDARD 11)
target_link_libraries(neural_network_opencl
ArrayFire::afopencl Threads::Threads)

add_executable(perceptron_opencl perceptron.cpp)
target_link_libraries(perceptron_opencl ArrayFire::afopencl)
Expand Down
67 changes: 51 additions & 16 deletions 67 examples/machine_learning/neural_network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
#include <math.h>
#include <stdio.h>
#include <af/util.h>
#include <list>
#include <string>
#include <thread>
#include <vector>
#include "mnist_common.h"

Expand Down Expand Up @@ -155,14 +157,15 @@ double ann::train(const array &input, const array &target, double alpha,

if (verbose) {
if ((i + 1) % 10 == 0)
printf("Epoch: %4d, Error: %0.4f\n", i + 1, err);
printf("Device: %d, Epoch: %4d, Error: %0.4f\n",
af::getDevice(), i + 1, err);
}
}
return err;
}

int ann_demo(bool console, int perc) {
printf("** ArrayFire ANN Demo **\n\n");
printf("Starting training on device %d ...\n\n", af::getDevice());

array train_images, test_images;
array train_target, test_target;
Expand Down Expand Up @@ -214,16 +217,16 @@ int ann_demo(bool console, int perc) {
af::sync();
double test_time = timer::stop() / 100;

printf("\nTraining set:\n");
printf("Accuracy on training data: %2.2f\n",
accuracy(train_output, train_target));
printf("\nAccuracy on training data: %2.2f device: %d\n",
accuracy(train_output, train_target), af::getDevice());

printf("\nTest set:\n");
printf("Accuracy on testing data: %2.2f\n",
accuracy(test_output, test_target));
printf("Accuracy on testing data: %2.2f device: %d\n",
accuracy(test_output, test_target), af::getDevice());

printf("\nTraining time: %4.4lf s\n", train_time);
printf("Prediction time: %4.4lf s\n\n", test_time);
printf("\nTraining time on device %d: %4.4lf s\n", af::getDevice(),
train_time);
printf("Prediction time on device %d: %4.4lf s\n\n", af::getDevice(),
test_time);

if (!console) {
// Get 20 random test images.
Expand All @@ -234,16 +237,48 @@ int ann_demo(bool console, int perc) {
return 0;
}

class learner {
public:
void learn(const unsigned d, const bool console, const int perc) {
printf("Starting new learner thread on device %d\n", d);
af::setDevice(d);
af::array r = af::randu(10);
ann_demo(console, perc);
}
};

int main(int argc, char **argv) {
int device = argc > 1 ? atoi(argv[1]) : 0;
bool console = argc > 2 ? argv[2][0] == '-' : false;
int perc = argc > 3 ? atoi(argv[3]) : 60;

int perc = argc > 3 ? atoi(argv[3]) : 60; // percentage training/test data
af::info();
const unsigned dc = af::getDeviceCount();
printf("** ArrayFire ANN Demo **\n\n");
printf("Usage: %s deviceId console percentage\n", argv[0]);
printf(
"- deviceId: either a device id (>= 0). If -1, 1 training will be "
"triggered per device\n");
printf("- console: console mode\n");
printf(
"- percentage: percent of training/testing data, default 60\% used for "
"training\n");
af::info();

std::list<learner> ls;
std::list<std::thread> ts;
try {
af::setDevice(device);
af::info();
return ann_demo(console, perc);

if (device < 0) {
for (unsigned i = 0; i < dc; ++i) {
ls.push_back(learner());
ts.push_back(
std::thread(&learner::learn, ls.back(), i, console, perc));
}
} else {
ls.push_back(learner());
ts.push_back(
std::thread(&learner::learn, ls.back(), device, console, perc));
}
for (auto &t : ts) t.join();
} catch (af::exception &ae) { std::cerr << ae.what() << std::endl; }

return 0;
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.