threadatomi.cpp

threadatomic.cpp
/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* ***************************************************************************
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 *  As a special exception, you may use this file as part of a free software
 *  library without restriction.  Specifically, if other files instantiate
 *  templates or use macros or inline functions from this file, or you compile
 *  this file and link it with other files to produce an executable, this
 *  file does not by itself cause the resulting executable to be covered by
 *  the GNU General Public License.  This exception does not however
 *  invalidate any other reasons why the executable file might be covered by
 *  the GNU General Public License.
 *
 ****************************************************************************
 */
 
#include <cmath>
#include <cstdint>
#include <climits>
#include <vector>
#include <atomic>
#include <iostream>
#include <thread>
 
// globals
std::vector<double> bigvector;  // input data
std::atomic<size_t> counter;    // atomic counter
std::vector<size_t> stats;      // collects  statistics
 
// executed by a thread
void do_work(size_t myid) {
 
    size_t idx = counter++;
    while(idx < bigvector.size())  {
        double &elem = bigvector[idx];
        elem = elem*sqrt(elem)+sin(elem)/(elem*sqrt(elem/2)) + 1.5;
        stats[myid]++;
        idx = counter++; // NOTE: the counter increment is atomic!
    }
}
 
void PrintV(const char str[]) {
    std::cout << str;
    for(size_t i=0;i<bigvector.size();++i)
        std::cout << bigvector[i] << " ";
    std::cout << "\n";
}
 
void PrintStats() {
    for(size_t i=0;i<stats.size();++i)
        std::cout << "thread " << i << " computes " << stats[i] << " tasks\n";
}
 
int main(int argc, char *argv[]) {
    if (argc!=3) {
        std::cerr << "use: "  << argv[0] << " numtasks nworkers\n";
        return -1;
    }
    size_t numtasks  = atol(argv[1]);
    size_t nworkers  = atoi(argv[2]);
 
    srandom(111);
 
    // initialize atomic counter
    counter = 0;
 
    // fill out the bigvector
    for(size_t i=0;i<numtasks;++i)
        bigvector.push_back(random()/(double)ULONG_MAX + 1.5);
 
    // initialize stats array
    for(size_t i=0;i<nworkers;++i)
        stats.push_back(0);
 
    std::vector<std::thread> pool;
 
    // starts all threads
    for(size_t i=0;i<nworkers;++i)
        pool.push_back(std::thread(do_work, i));
 
    // wait all threads
    for(size_t i=0;i<nworkers;++i)
        pool[i].join();
 
    //PrintV("after:\n");
    PrintStats();
    return 0;
}