0

I already checked the following questions 1, 2, 3, 4, and 5 but the answers didn't help. I have tried to compile the following C++ program taken from here,

#include <mpi.h>
#include <cstdio>
#include <chrono>
#include <vector>
#include <numeric>

#define ITERATIONS 10
#define NS2MS(X) (X / 1000000.0)

static int rank(0), world_size(0);

double seq_pi(unsigned long N) {
    double step = 1.0 / N;
    double sum = 0.0;
    for (unsigned long i = 0; i < N; i++) {
        double x = (i + 0.5) * step;
        sum += (4.0 / (1.0 + x * x));
    }
    return sum * step;
}

double pi(const unsigned long N) {
    const double step = 1.0 / N;
    double sum = 0.0;
    double end_sum = 0.0;
    const long i_per = N / world_size;
    for (unsigned long i = rank * i_per; i < (rank + 1) * i_per; i++) {
        double x = (i + 0.5) * step;
        sum += (4.0 / (1.0 + x * x));
    }
    MPI_Reduce(&sum, &end_sum, 1, MPI_DOUBLE, MPI_SUM, 0,
               MPI_COMM_WORLD);
    return end_sum * step;
}

int main(int argc, char **argv) {
    std::vector<int> problem_sizes = {1000000, 10000000, 100000000};
    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &world_size);
#if defined (SCALING_TEST)
    {
     int N = 100000000;
#else
    for (int N: problem_sizes) {
#endif
        std::chrono::time_point
                <std::chrono::high_resolution_clock> start, end;
        std::chrono::nanoseconds runtime[ITERATIONS];
        double aprox, seq_aprox;
#if defined (SCALING_TEST)
        for(int iter(0); iter < ITERATIONS; iter++) {
#endif
        start = std::chrono::high_resolution_clock::now();
        seq_aprox = seq_pi(N);
        end = std::chrono::high_resolution_clock::now();
#if defined (SCALING_TEST)
        runtime[iter] = std::chrono::duration_cast
        <std::chrono::nanoseconds>(end - start);
        }
        auto seq_time = std::accumulate(runtime, runtime + ITERATIONS, std::chrono::nanoseconds(0));
        auto seq_average = seq_time / ITERATIONS;
#else
        auto seq_time = runtime[0];
        auto seq_average = seq_time;
#endif
        for (auto &iter: runtime) {
            MPI_Barrier(MPI_COMM_WORLD);
            start = std::chrono::high_resolution_clock::now();
            aprox = pi(N);
            end = std::chrono::high_resolution_clock::now();
            iter = std::chrono::duration_cast
                    <std::chrono::nanoseconds>(end - start);
        }
        auto par_time = std::accumulate(runtime,
                                        runtime + ITERATIONS, std::chrono::nanoseconds(0));
        auto par_average = par_time / ITERATIONS;
        auto diff = std::fabs(aprox - seq_aprox);
        if (rank == 0) {
#if defined (SCALING_TEST)
            auto ratio = seq_average.count() / double(par_average.count());
            printf("%d \t %f \t %f \t %e\n", world_size,
                   NS2MS(par_average.count()), ratio, diff);
#else
            printf("%0.0e \t %f \t %f \n", N + 0.0,
                   NS2MS(par_average.count()), diff);
#endif
        }
    }
    MPI_Finalize();
    return 0;
}

But I get the following error:

Undefined symbols for architecture x86_64:
  "_MPI_Barrier", referenced from:
      _main in main-7ef8bb.o
  "_MPI_Comm_rank", referenced from:
      _main in main-7ef8bb.o
  "_MPI_Comm_size", referenced from:
      _main in main-7ef8bb.o
  "_MPI_Finalize", referenced from:
      _main in main-7ef8bb.o
  "_MPI_Init", referenced from:
      _main in main-7ef8bb.o
  "_MPI_Reduce", referenced from:
      pi(unsigned long) in main-7ef8bb.o
  "_ompi_mpi_comm_world", referenced from:
      pi(unsigned long) in main-7ef8bb.o
      _main in main-7ef8bb.o
  "_ompi_mpi_double", referenced from:
      pi(unsigned long) in main-7ef8bb.o
  "_ompi_mpi_op_sum", referenced from:
      pi(unsigned long) in main-7ef8bb.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I am using macOS, the project is called Pi_Approximation and the file to be compiled is main.cpp. There are no helpers.cpp files in the project. I tried following commands one by one to compile the code but got the above error each time:

mpicc -o Pi_Approximation main.cpp -std=c++11

g++ -std=c++11 main.cpp

clang++ -std=c++11 main.cpp

This is the CMakeList.txt:

cmake_minimum_required(VERSION 3.22)
project(Pi_Approximation)

set(CMAKE_CXX_STANDARD 23)

find_package(MPI REQUIRED)
set(MPI_EXECUTABLE_SUFFIX ".mpich")

include_directories(${MPI_INCLUDE_PATH})

add_executable(Pi_Approximation main.cpp)
target_link_libraries(Pi_Approximation ${MPI_LIBRARIES})

if(MPI_COMPILE_FLAGS)
    set_target_properties(Pi_Approximation PROPERTIES
            COMPILE_FLAGS "${MPI_COMPILE_FLAGS}")
endif()

if(MPI_LINK_FLAGS)
    set_target_properties(Pi_Approximation PROPERTIES
            LINK_FLAGS "${MPI_LINK_FLAGS}")
endif()

How do I fix the error?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
plpm
  • 539
  • 3
  • 12

0 Answers0