Skip to content

Commit

Permalink
Revert "seventh trial"
Browse files Browse the repository at this point in the history
This reverts commit e01b669.
  • Loading branch information
Niall Douglas (s [underscore] sourceforge {at} nedprod [dot] com) committed Feb 6, 2015
1 parent e01b669 commit ed44043
Show file tree
Hide file tree
Showing 67 changed files with 9,908 additions and 11,281 deletions.
59 changes: 31 additions & 28 deletions example/adopt_example.cpp
Expand Up @@ -3,37 +3,40 @@
//[adopt_example
struct test_handle : boost::afio::async_io_handle
{
test_handle(boost::afio::async_file_io_dispatcher_base *parent)
: boost::afio::async_io_handle(parent,
std::shared_ptr<boost::afio::async_io_handle>(),
"foo", boost::afio::file_flags::None)
{
}
virtual void close()
{
// Do nothing
}
virtual void *native_handle() const { return nullptr; }
virtual boost::afio::directory_entry
direntry(boost::afio::metadata_flags
wanted = boost::afio::directory_entry::metadata_fastpath()) const
{
return boost::afio::directory_entry();
}
virtual boost::afio::filesystem::path target() const
{
return boost::afio::filesystem::path();
}
virtual void *try_mapfile() { return nullptr; }
test_handle(boost::afio::async_file_io_dispatcher_base *parent) :
boost::afio::async_io_handle(parent,
std::shared_ptr<boost::afio::async_io_handle>(),
"foo", boost::afio::file_flags::None) {}
virtual void close()
{
// Do nothing
}
virtual void *native_handle() const
{
return nullptr;
}
virtual boost::afio::directory_entry direntry(boost::afio::metadata_flags
wanted=boost::afio::directory_entry::metadata_fastpath()) const
{
return boost::afio::directory_entry();
}
virtual boost::afio::filesystem::path target() const
{
return boost::afio::filesystem::path();
}
virtual void *try_mapfile()
{
return nullptr;
}
};

int main(void)
{
auto dispatcher =
boost::afio::make_async_file_io_dispatcher(boost::afio::process_threadpool());
auto h = std::make_shared<test_handle>(dispatcher.get());
auto adopted = dispatcher->adopt(h);
when_all(adopted).wait();
return 0;
auto dispatcher = boost::afio::make_async_file_io_dispatcher(
boost::afio::process_threadpool());
auto h=std::make_shared<test_handle>(dispatcher.get());
auto adopted=dispatcher->adopt(h);
when_all(adopted).wait();
return 0;
}
//]
125 changes: 59 additions & 66 deletions example/barrier_example.cpp
Expand Up @@ -2,73 +2,66 @@

int main(void)
{
//[barrier_example
// Assume that groups is 10,000 items long with item.first being randomly
// between 1 and 500. This example is adapted from the barrier() unit test.
//
// What we're going to do is this: for each item in groups, schedule item.first
// parallel ops and a barrier which completes only when the last of that
// parallel group completes. Chain the next group to only execute after the
// preceding group's barrier completes. Repeat until all groups have been executed.
std::shared_ptr<boost::afio::async_file_io_dispatcher_base> dispatcher =
boost::afio::make_async_file_io_dispatcher();
std::vector<std::pair<size_t, int>> groups;
boost::afio::atomic<size_t> callcount[10000];
memset(&callcount, 0, sizeof(callcount));

// This lambda is what each parallel op in each group will do: increment an atomic
// for that group.
auto inccount = [](boost::afio::atomic<size_t> *count)
{
(*count)++;
};

// This lambda is called after each barrier completes, and it checks that exactly
// the right number of inccount lambdas were executed.
auto verifybarrier = [](boost::afio::atomic<size_t> *count, size_t shouldbe)
{
if(*count != shouldbe)
throw std::runtime_error("Count was not what it should have been!");
return true;
};

// For each group, dispatch ops and a barrier for them
boost::afio::async_io_op next;
bool isfirst = true;
for(auto &run : groups)
{
// Create a vector of run.first size of bound inccount lambdas
// This will be the batch issued for this group
std::vector<std::function<void()>> thisgroupcalls(
run.first, std::bind(inccount, &callcount[run.second]));
std::vector<boost::afio::async_io_op> thisgroupcallops;
// If this is the first item, schedule without precondition
if(isfirst)
//[barrier_example
// Assume that groups is 10,000 items long with item.first being randomly
// between 1 and 500. This example is adapted from the barrier() unit test.
//
// What we're going to do is this: for each item in groups, schedule item.first
// parallel ops and a barrier which completes only when the last of that
// parallel group completes. Chain the next group to only execute after the
// preceding group's barrier completes. Repeat until all groups have been executed.
std::shared_ptr<boost::afio::async_file_io_dispatcher_base> dispatcher=
boost::afio::make_async_file_io_dispatcher();
std::vector<std::pair<size_t, int>> groups;
boost::afio::atomic<size_t> callcount[10000];
memset(&callcount, 0, sizeof(callcount));

// This lambda is what each parallel op in each group will do: increment an atomic
// for that group.
auto inccount = [](boost::afio::atomic<size_t> *count){ (*count)++; };

// This lambda is called after each barrier completes, and it checks that exactly
// the right number of inccount lambdas were executed.
auto verifybarrier = [](boost::afio::atomic<size_t> *count, size_t shouldbe)
{
thisgroupcallops = dispatcher->call(thisgroupcalls).second;
isfirst = false;
}
else
if (*count != shouldbe)
throw std::runtime_error("Count was not what it should have been!");
return true;
};

// For each group, dispatch ops and a barrier for them
boost::afio::async_io_op next;
bool isfirst = true;
for(auto &run : groups)
{
// Create a vector of run.first size of preconditions exactly
// matching the number in this batch. Note that the precondition
// for all of these is the preceding verify op
std::vector<boost::afio::async_io_op> dependency(run.first, next);
thisgroupcallops = dispatcher->call(dependency, thisgroupcalls).second;
// Create a vector of run.first size of bound inccount lambdas
// This will be the batch issued for this group
std::vector<std::function<void()>> thisgroupcalls(run.first, std::bind(inccount, &callcount[run.second]));
std::vector<boost::afio::async_io_op> thisgroupcallops;
// If this is the first item, schedule without precondition
if (isfirst)
{
thisgroupcallops = dispatcher->call(thisgroupcalls).second;
isfirst = false;
}
else
{
// Create a vector of run.first size of preconditions exactly
// matching the number in this batch. Note that the precondition
// for all of these is the preceding verify op
std::vector<boost::afio::async_io_op> dependency(run.first, next);
thisgroupcallops = dispatcher->call(dependency, thisgroupcalls).second;
}
// barrier() is very easy: its number of output ops exactly matches its input
// but none of the output will complete until the last of the input completes
auto thisgroupbarriered = dispatcher->barrier(thisgroupcallops);
// Schedule a call of the verify lambda once barrier completes. Here we choose
// the first item of the barrier's return, but in truth any of them are good.
auto verify = dispatcher->call(thisgroupbarriered.front(), std::function<bool()>(std::bind(verifybarrier, &callcount[run.second], run.first)));
// Set the dependency for the next batch to be the just scheduled verify op
next = verify.second;
}
// barrier() is very easy: its number of output ops exactly matches its input
// but none of the output will complete until the last of the input completes
auto thisgroupbarriered = dispatcher->barrier(thisgroupcallops);
// Schedule a call of the verify lambda once barrier completes. Here we choose
// the first item of the barrier's return, but in truth any of them are good.
auto verify =
dispatcher->call(thisgroupbarriered.front(),
std::function<bool()>(
std::bind(verifybarrier, &callcount[run.second], run.first)));
// Set the dependency for the next batch to be the just scheduled verify op
next = verify.second;
}
// next was the last op scheduled, so waiting on it waits on everything
when_all(next).wait();
//]
// next was the last op scheduled, so waiting on it waits on everything
when_all(next).wait();
//]
}
52 changes: 24 additions & 28 deletions example/benchmark_asio.cpp
Expand Up @@ -10,42 +10,38 @@ static int callback()
#if 0
Sleep(0);
#endif
--togo;
return 1;
--togo;
return 1;
};
int main(void)
{
using namespace boost::afio;
typedef chrono::duration<double, ratio<1, 1>> secs_type;
auto threadpool = process_threadpool();
auto begin = chrono::high_resolution_clock::now();
while(chrono::duration_cast<secs_type>(chrono::high_resolution_clock::now() -
begin).count() < 3)
;

atomic<size_t> threads(0);
using namespace boost::afio;
typedef chrono::duration<double, ratio<1, 1>> secs_type;
auto threadpool=process_threadpool();
auto begin=chrono::high_resolution_clock::now();
while(chrono::duration_cast<secs_type>(chrono::high_resolution_clock::now()-begin).count()<3);

atomic<size_t> threads(0);
#if 0
std::cout << "Attach profiler now and hit Return" << std::endl;
getchar();
#endif
begin = chrono::high_resolution_clock::now();
begin=chrono::high_resolution_clock::now();
#pragma omp parallel
{
++threads;
for(size_t n = 0; n < 5000000; n++)
{
++togo;
threadpool->enqueue(callback);
++threads;
for(size_t n=0; n<5000000; n++)
{
++togo;
threadpool->enqueue(callback);
}
}
}
while(togo)
this_thread::sleep_for(chrono::milliseconds(1));
auto end = chrono::high_resolution_clock::now();
auto diff = chrono::duration_cast<secs_type>(end - begin);
std::cout << "It took " << diff.count() << " secs to execute "
<< (5000000 * threads) << " closures which is "
<< (5000000 * threads / diff.count()) << " closures/sec" << std::endl;
std::cout << "\nPress Return to exit ..." << std::endl;
getchar();
return 0;
while(togo)
this_thread::sleep_for(chrono::milliseconds(1));
auto end=chrono::high_resolution_clock::now();
auto diff=chrono::duration_cast<secs_type>(end-begin);
std::cout << "It took " << diff.count() << " secs to execute " << (5000000*threads) << " closures which is " << (5000000*threads/diff.count()) << " closures/sec" << std::endl;
std::cout << "\nPress Return to exit ..." << std::endl;
getchar();
return 0;
}

0 comments on commit ed44043

Please sign in to comment.