Skip to content

Instantly share code, notes, and snippets.

@9prady9
Last active March 8, 2017 13:13
Show Gist options
  • Save 9prady9/6e5b5dcf37c18f63aa5c597e24a886a4 to your computer and use it in GitHub Desktop.
Save 9prady9/6e5b5dcf37c18f63aa5c597e24a886a4 to your computer and use it in GitHub Desktop.
ArrayFire example to split work across multiple GPUs - No threads Used.
/*******************************************************
* Copyright (c) 2017, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <arrayfire.h>
#include <chrono>
using namespace af;
using std::vector;
using std::string;
int main(void)
{
vector<bool> isDilationFlags;
vector<bool> isColorFlags;
vector<string> files;
files.push_back( string("<path to image file>") );
isDilationFlags.push_back(true);
isColorFlags.push_back(false);
files.push_back( string("<path to image file>") );
isDilationFlags.push_back(false);
isColorFlags.push_back(true);
auto start = std::chrono::high_resolution_clock::now();
for(int pos = 0; pos<files.size(); ++pos)
{
const bool isDilation = isDilationFlags[pos];
const bool isColor = isColorFlags[pos];
const dim4 maskdims(3,3,1,1);
// Need to set the device before data is created or
// loaded onto the memory of the GPU to which
// work has to be submitted
int trgtDeviceId = pos % af::getDeviceCount();
af::setDevice(trgtDeviceId);
const array mask = constant(1.0, maskdims);
// af::loadImage is blocking call since it loads
// data from disk to GPU memory
array input = loadImage(files[pos].c_str(), isColor);
// all function calls in ArrayFire are asynchronous in nature
// unless they are copying data to or from GPU
af::array out = isDilation ? dilate(input, mask) : erode(input, mask);
// Process the next image: files[pos+1]
}
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = end - start;
std::cout << "Total time taken for test : " << diff.count() << " s\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment