0

I am trying to write a parallel bubblesort function. I am running into an error when using boost::bind:

void swap(vector<int>& input, int i, int j)
{
    if (input[i] > input[j])
    {   
        int temp = input[j];
        input[j] = input[i];
        input[i] = temp;
    }   
    return;
}

void parallel_bubblesort(vector<int>& input, int, int)
{

    int i, j, temp;

    for (i = 0; i < input.size(); i++)
    {   
        boost::asio::io_service ioService;
        boost::thread_group threadpool;
        for (j = 0; j < input.size() - 1; j++)
        {   
            ioService.post(boost::bind(&swap, boost::ref(input), 2 * j + i % 2, 2 * j + 1 + i % 2));
        }   

        for (int t = 0; t < NUM_THREADS; t++)
        {   
            threadpool.create_thread(boost::bind(&boost::asio::io_service::run, &ioService));
        }   

        threadpool.join_all();
        ioService.stop();
    }   
    return;
}

It snags on the line

ioService.post(boost::bind(&swap, boost::ref(input), 2 * j + i % 2, 2 * j + 1 + i % 2));

The error says

no matching function for call to ‘bind(<unresolved overloaded function type>, std::vector<int, std::allocator<int> >&, int, int)’

This seems weird, because none of these are overloaded at all, does anybody know what this means?

Edit: Here is the full program

#include <iostream>
#include <chrono>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <algorithm>
#include <boost/thread.hpp>
#include <boost/asio/io_service.hpp>
#define NUM_THREADS 4

using namespace std;
using namespace boost;
using namespace boost::this_thread;

int check_solution(const vector<int>&);
void bubblesort(vector<int>&);
void swap1(vector<int>&, int, int);
void parallel_bubblesort(vector<int>&);

int main()
{
    string line, token;
    string file = "test.txt";
    vector<int> unsorted_integers;

    //print out filename
    cout << file << endl;

    ifstream myfile(file);

    //open file
    if(myfile.is_open())
    {   
        //load everything into a vector
        while (getline(myfile, line))
        {   
            int temp;
            std::istringstream ss(line);
            std::getline(ss, token, '\n');
            temp = atoi(token.c_str());
            unsorted_integers.push_back(temp);
        }   
    }   

    vector<int> unsorted_integers2(unsorted_integers);
 //starts clock
    std::chrono::high_resolution_clock::time_point start2 = std::chrono::high_resolution_clock::now();

    //run the sort function and check for correctness
    parallel_bubblesort(unsorted_integers2);
    if(check_solution(unsorted_integers2))
    {
        cout << "The parallel sort solution is correct" << endl;
    }
    else
    {
        cout << "The parallel sort solution is incorrect" << endl;
    }

    //stops clock and prints times
    std::chrono::high_resolution_clock::time_point end2 = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double> total_time2 = std::chrono::duration_cast<std::chrono::duration<double>>(end2 - start2);
    cout << "The parallel sort function took " << total_time2.count() << " seconds." << endl;

    return 0;
}


void swap1(vector<int>& input, int i, int j)
{
    if (input[i] > input[j])
    {
        int temp = input[j];
        input[j] = input[i];
        input[i] = temp;
    }
    return;
}

void parallel_bubblesort(vector<int>& input, int, int)
{

    int i, j;

    for (i = 0; i < input.size(); i++)
      {
        boost::asio::io_service ioService;
        boost::thread_group threadpool;
        for (j = 0; j < input.size() - 1; j++)
        {
            ioService.post(boost::bind(&swap1, boost::ref(input), 2 * j + i % 2, 2 * j + 1 + i % 2));
        }

        for (int t = 0; t < NUM_THREADS; t++)
        {
            threadpool.create_thread(boost::bind(&boost::asio::io_service::run, &ioService));
        }

        threadpool.join_all();
        ioService.stop();
    }
    return;
}

int check_solution(const vector<int>& solution)
{
    vector<int> correct_solution(solution);
    std::sort(correct_solution.begin(), correct_solution.end());
    return (solution == correct_solution);
}
Dan
  • 2,647
  • 2
  • 27
  • 37
  • What compiler and boost version are you using? VC12 with Boost 1.55 has no problem compiling this. You could also may be missing an `include`, it may help if you provided a ready to try snippet with your `include`s. – Alexey Biryukov May 07 '14 at 05:45
  • I am using the command g++ bubblesort.cpp -std = c++0x -boost_thread-mt -lboost_system – Dan May 07 '14 at 05:46
  • Add -Wall -Werror to your compilation line. – John Zwinck May 07 '14 at 05:58
  • Oh cool trick, this seems promising this was the output: bubblesort.cpp: In function ‘void bubblesort(std::vector >&)’: bubblesort.cpp:94: error: comparison between signed and unsigned integer expressions bubblesort.cpp:96: error: comparison between signed and unsigned integer expressions bubblesort.cpp:92: error: unused variable ‘temp’ bubblesort.cpp: In function ‘void parallel_bubblesort(std::vector >&, int, int)’: bubblesort.cpp:120: error: comparison between signed and unsigned integer expressions – Dan May 07 '14 at 06:00
  • Actually, yeah these warnings don't seem related to the problem – Dan May 07 '14 at 06:01
  • Your current code builds just fine, with only 2 warnings. gcc 4.8.2.1, boost 1.54. `g++ -c -std=c++11 -Wall -Wno-unused-local-typedefs -g -gdwarf-2 -D_DEBUG bubblesort.cpp` – Alexey Biryukov May 07 '14 at 06:44
  • Cool, this is compiling now thanks so much for all your help! – Dan May 07 '14 at 06:50
  • So what have you done to make it build finally? – Alexey Biryukov May 07 '14 at 07:06
  • I am not exactly sure.....my environment doesn't recognize the -std=c++11 flag and none of the others seem like they should be affecting anything, maybe one of my old compiler flags was messing something up? – Dan May 07 '14 at 07:09
  • Anyways I would advise you to mark John's reply as an answer because he has correctly identified the root cause. – Alexey Biryukov May 07 '14 at 07:20

1 Answers1

2

You are probably doing using namespace std; somewhere, and std::swap is a thing. I suggest you simply rename your swap function to something else.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436