/*****************************************************************************

YASK: Yet Another Stencil Kit
Copyright (c) 2014-2023, Intel Corporation

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

* The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.

*****************************************************************************/

#include "yask_stencil.hpp"
using namespace std;

// Set MODEL_CACHE to 1 or 2 to model that cache level
// and create a global cache object here.
#ifdef MODEL_CACHE
Cache cache_model(MODEL_CACHE);
#endif

namespace yask {

    ////// MPI utils //////

    // Exit properly.
    void KernelEnv::exit(int code) {

#ifdef USE_MPI
        if (code == 0)
            finalize();

        else {
            int flag;
            MPI_Initialized(&flag);
            if (flag) {
                int num_ranks = 1;
                MPI_Comm_size(MPI_COMM_WORLD, &num_ranks);
                if (num_ranks > 1)
                    MPI_Abort(MPI_COMM_WORLD, code);
            }
        }
#endif
        ::exit(code);
    }
    
    void KernelEnv::finalize() {
        TRACE_MSG("finalize_needed = " << finalize_needed);
        if (comm != MPI_COMM_NULL && finalize_needed) {
            MPI_Finalize();
            comm = MPI_COMM_NULL;
            shm_comm = MPI_COMM_NULL;
        }
        finalize_needed = false;
    }
    
    // Find sum of rank_vals over all ranks.
    idx_t KernelEnv::sum_over_ranks(idx_t rank_val) const {
        idx_t sum_val = rank_val;
#ifdef USE_MPI
        MPI_Allreduce(&rank_val, &sum_val, 1, MPI_INTEGER8, MPI_SUM, comm);
#endif
        return sum_val;
    }

    // Make sure rank_val is same over all ranks.
    void KernelEnv::assert_equality_over_ranks(idx_t rank_val,
                                               const string& descr) const {
        idx_t min_val = rank_val;
        idx_t max_val = rank_val;
#ifdef USE_MPI
        MPI_Allreduce(&rank_val, &min_val, 1, MPI_INTEGER8, MPI_MIN, comm);
        MPI_Allreduce(&rank_val, &max_val, 1, MPI_INTEGER8, MPI_MAX, comm);
#endif

        if (min_val != rank_val || max_val != rank_val) {
            FORMAT_AND_THROW_YASK_EXCEPTION(descr << " ranges from " << min_val << " to " <<
                                            max_val << " across the ranks; they should all be identical");
        }
    }

}
