<commit_before>/*
 * Distributed under the OSI-approved Apache License, Version 2.0.  See
 * accompanying file Copyright.txt for details.
 *
 * ADIOS is freely available under the terms of the BSD license described
 * in the COPYING file in the top level directory of this source distribution.
 *
 * Copyright (c) 2008 - 2009.  UT-BATTELLE, LLC. All rights reserved.

   A dummy MPI implementation for the BP READ API, to have an MPI-free version
   of the API
*/

#include "mpidummy.h"

#include <cinttypes>
#include <cstdio>
#include <cstring>
#include <numeric>

#include <chrono>
#include <string>

// remove warnings on Windows
#ifdef _WIN32
#pragma warning(disable : 4996) // fopen
#pragma warning(disable : 4477) // strcpy, sprintf
#endif

namespace adios2
{
namespace helper
{
namespace mpi
{

static char mpierrmsg[MPI_MAX_ERROR_STRING];

int MPI_Init(int * /*argc*/, char *** /*argv*/)
{
    mpierrmsg[0] = '\0';
    return MPI_SUCCESS;
}

int MPI_Finalize()
{
    mpierrmsg[0] = '\0';
    return MPI_SUCCESS;
}

int MPI_Initialized(int *flag)
{
    *flag = 1;
    return MPI_SUCCESS;
}

int MPI_Finalized(int *flag)
{
    *flag = 0;
    return MPI_SUCCESS;
}

int MPI_Comm_split(MPI_Comm /*comm*/, int /*color*/, int /*key*/,
                   MPI_Comm * /*comm_out*/)
{
    return MPI_SUCCESS;
}

int MPI_Barrier(MPI_Comm /*comm*/) { return MPI_SUCCESS; }

int MPI_Bcast(void * /*buffer*/, int /*count*/, MPI_Datatype /*datatype*/,
              int /*root*/, MPI_Comm /*comm*/)
{
    return MPI_SUCCESS;
}

int MPI_Comm_dup(MPI_Comm comm, MPI_Comm *newcomm)
{
    *newcomm = comm;
    return MPI_SUCCESS;
}

int MPI_Comm_rank(MPI_Comm /*comm*/, int *rank)
{
    *rank = 0;
    return MPI_SUCCESS;
}

int MPI_Comm_size(MPI_Comm /*comm*/, int *size)
{
    *size = 1;
    return MPI_SUCCESS;
}

int MPI_Comm_free(MPI_Comm *comm)
{
    *comm = 0;
    return MPI_SUCCESS;
}

MPI_Comm MPI_Comm_f2c(MPI_Fint comm) { return comm; }

int MPI_Gather(const void *sendbuf, int sendcnt, MPI_Datatype sendtype,
               void *recvbuf, int recvcnt, MPI_Datatype recvtype, int root,
               MPI_Comm comm)
{
    int ier = MPI_SUCCESS;
    size_t n = 0, nsent = 0, nrecv = 0;
    if (!sendbuf && !recvbuf)
    {
        return ier;
    }
    if (comm == MPI_COMM_NULL || root)
    {
        ier = MPI_ERR_COMM;
    }

    switch (sendtype)
    {
    case MPI_CHAR:
        n = sizeof(char);
        break;
    case MPI_INT:
        n = sizeof(int);
        break;
    case MPI_UNSIGNED:
        n = sizeof(unsigned int);
        break;
    case MPI_UNSIGNED_LONG:
        n = sizeof(unsigned long);
        break;
    case MPI_UNSIGNED_LONG_LONG:
        n = sizeof(unsigned long long);
        break;
    default:
        return MPI_ERR_TYPE;
    }
    nsent = n * sendcnt;

    switch (recvtype)
    {
    case MPI_CHAR:
        nrecv = sizeof(char);
        break;
    case MPI_INT:
        nrecv = sizeof(int);
        break;
    case MPI_UNSIGNED:
        nrecv = sizeof(unsigned int);
        break;
    case MPI_UNSIGNED_LONG:
        nrecv = sizeof(unsigned long);
        break;
    case MPI_UNSIGNED_LONG_LONG:
        nrecv = sizeof(unsigned long long);
        break;
    default:
        return MPI_ERR_TYPE;
    }
    nrecv = n * recvcnt;

    if (nrecv != nsent)
    {
        ier = MPI_ERR_COUNT;
    }

    if (ier == MPI_SUCCESS)
    {
        std::memcpy(recvbuf, sendbuf, nsent);
    }
    else
    {
        std::snprintf(mpierrmsg, ier, "could not gather data\n");
    }

    return ier;
}

int MPI_Gatherv(const void *sendbuf, int sendcnt, MPI_Datatype sendtype,
                void *recvbuf, const int *recvcnts, const int * /*displs */,
                MPI_Datatype recvtype, int root, MPI_Comm comm)
{
    int ier = MPI_SUCCESS;
    if (*recvcnts != sendcnt)
    {
        ier = MPI_ERR_BUFFER;
        return ier;
    }

    ier = MPI_Gather(sendbuf, sendcnt, sendtype, recvbuf, *recvcnts, recvtype,
                     root, comm);
    return ier;
}

int MPI_Allgather(const void *sendbuf, int sendcount, MPI_Datatype sendtype,
                  void *recvbuf, int recvcount, MPI_Datatype recvtype,
                  MPI_Comm comm)
{
    return MPI_Gather(sendbuf, sendcount, sendtype, recvbuf, recvcount,
                      recvtype, 0, comm);
}

int MPI_Scatter(const void *sendbuf, int sendcnt, MPI_Datatype sendtype,
                void *recvbuf, int recvcnt, MPI_Datatype recvtype, int root,
                MPI_Comm comm)
{
    int ier = MPI_SUCCESS;
    size_t n = 0, nsent = 0, nrecv = 0;
    if (!sendbuf || !recvbuf)
    {
        ier = MPI_ERR_BUFFER;
    }

    if (comm == MPI_COMM_NULL || root)
    {
        ier = MPI_ERR_COMM;
    }

    switch (sendtype)
    {
    case MPI_INT:
        n = sizeof(int);
        break;
    default:
        return MPI_ERR_TYPE;
    }
    nsent = n * sendcnt;

    switch (recvtype)
    {
    case MPI_INT:
        nrecv = sizeof(int);
        break;
    default:
        return MPI_ERR_TYPE;
    }
    nrecv = n * recvcnt;

    if (nrecv != nsent)
    {
        ier = MPI_ERR_COUNT;
    }

    if (ier == MPI_SUCCESS)
    {
        std::memcpy(recvbuf, sendbuf, nsent);
    }
    else
    {
        std::snprintf(mpierrmsg, ier, "could not scatter data\n");
    }

    return ier;
}

int MPI_Scatterv(const void *sendbuf, const int *sendcnts, const int *displs,
                 MPI_Datatype sendtype, void *recvbuf, int recvcnt,
                 MPI_Datatype recvtype, int root, MPI_Comm comm)
{
    int ier = MPI_SUCCESS;
    if (!sendcnts || !displs)
    {
        ier = MPI_ERR_BUFFER;
    }<commit_msg>mpidummy: proper size in snprintf<commit_after>/*
 * Distributed under the OSI-approved Apache License, Version 2.0.  See
 * accompanying file Copyright.txt for details.
 *
 * ADIOS is freely available under the terms of the BSD license described
 * in the COPYING file in the top level directory of this source distribution.
 *
 * Copyright (c) 2008 - 2009.  UT-BATTELLE, LLC. All rights reserved.

   A dummy MPI implementation for the BP READ API, to have an MPI-free version
   of the API
*/

#include "mpidummy.h"

#include <cinttypes>
#include <cstdio>
#include <cstring>
#include <numeric>

#include <chrono>
#include <string>

// remove warnings on Windows
#ifdef _WIN32
#pragma warning(disable : 4996) // fopen
#pragma warning(disable : 4477) // strcpy, sprintf
#endif

namespace adios2
{
namespace helper
{
namespace mpi
{

static char mpierrmsg[MPI_MAX_ERROR_STRING];

int MPI_Init(int * /*argc*/, char *** /*argv*/)
{
    mpierrmsg[0] = '\0';
    return MPI_SUCCESS;
}

int MPI_Finalize()
{
    mpierrmsg[0] = '\0';
    return MPI_SUCCESS;
}

int MPI_Initialized(int *flag)
{
    *flag = 1;
    return MPI_SUCCESS;
}

int MPI_Finalized(int *flag)
{
    *flag = 0;
    return MPI_SUCCESS;
}

int MPI_Comm_split(MPI_Comm /*comm*/, int /*color*/, int /*key*/,
                   MPI_Comm * /*comm_out*/)
{
    return MPI_SUCCESS;
}

int MPI_Barrier(MPI_Comm /*comm*/) { return MPI_SUCCESS; }

int MPI_Bcast(void * /*buffer*/, int /*count*/, MPI_Datatype /*datatype*/,
              int /*root*/, MPI_Comm /*comm*/)
{
    return MPI_SUCCESS;
}

int MPI_Comm_dup(MPI_Comm comm, MPI_Comm *newcomm)
{
    *newcomm = comm;
    return MPI_SUCCESS;
}

int MPI_Comm_rank(MPI_Comm /*comm*/, int *rank)
{
    *rank = 0;
    return MPI_SUCCESS;
}

int MPI_Comm_size(MPI_Comm /*comm*/, int *size)
{
    *size = 1;
    return MPI_SUCCESS;
}

int MPI_Comm_free(MPI_Comm *comm)
{
    *comm = 0;
    return MPI_SUCCESS;
}

MPI_Comm MPI_Comm_f2c(MPI_Fint comm) { return comm; }

int MPI_Gather(const void *sendbuf, int sendcnt, MPI_Datatype sendtype,
               void *recvbuf, int recvcnt, MPI_Datatype recvtype, int root,
               MPI_Comm comm)
{
    int ier = MPI_SUCCESS;
    size_t n = 0, nsent = 0, nrecv = 0;
    if (!sendbuf && !recvbuf)
    {
        return ier;
    }
    if (comm == MPI_COMM_NULL || root)
    {
        ier = MPI_ERR_COMM;
    }

    switch (sendtype)
    {
    case MPI_CHAR:
        n = sizeof(char);
        break;
    case MPI_INT:
        n = sizeof(int);
        break;
    case MPI_UNSIGNED:
        n = sizeof(unsigned int);
        break;
    case MPI_UNSIGNED_LONG:
        n = sizeof(unsigned long);
        break;
    case MPI_UNSIGNED_LONG_LONG:
        n = sizeof(unsigned long long);
        break;
    default:
        return MPI_ERR_TYPE;
    }
    nsent = n * sendcnt;

    switch (recvtype)
    {
    case MPI_CHAR:
        nrecv = sizeof(char);
        break;
    case MPI_INT:
        nrecv = sizeof(int);
        break;
    case MPI_UNSIGNED:
        nrecv = sizeof(unsigned int);
        break;
    case MPI_UNSIGNED_LONG:
        nrecv = sizeof(unsigned long);
        break;
    case MPI_UNSIGNED_LONG_LONG:
        nrecv = sizeof(unsigned long long);
        break;
    default:
        return MPI_ERR_TYPE;
    }
    nrecv = n * recvcnt;

    if (nrecv != nsent)
    {
        ier = MPI_ERR_COUNT;
    }

    if (ier == MPI_SUCCESS)
    {
        std::memcpy(recvbuf, sendbuf, nsent);
    }
    else
    {
        std::snprintf(mpierrmsg, MPI_MAX_ERROR_STRING,
                      "could not gather data\n");
    }

    return ier;
}

int MPI_Gatherv(const void *sendbuf, int sendcnt, MPI_Datatype sendtype,
                void *recvbuf, const int *recvcnts, const int * /*displs */,
                MPI_Datatype recvtype, int root, MPI_Comm comm)
{
    int ier = MPI_SUCCESS;
    if (*recvcnts != sendcnt)
    {
        ier = MPI_ERR_BUFFER;
        return ier;
    }

    ier = MPI_Gather(sendbuf, sendcnt, sendtype, recvbuf, *recvcnts, recvtype,
                     root, comm);
    return ier;
}

int MPI_Allgather(const void *sendbuf, int sendcount, MPI_Datatype sendtype,
                  void *recvbuf, int recvcount, MPI_Datatype recvtype,
                  MPI_Comm comm)
{
    return MPI_Gather(sendbuf, sendcount, sendtype, recvbuf, recvcount,
                      recvtype, 0, comm);
}

int MPI_Scatter(const void *sendbuf, int sendcnt, MPI_Datatype sendtype,
                void *recvbuf, int recvcnt, MPI_Datatype recvtype, int root,
                MPI_Comm comm)
{
    int ier = MPI_SUCCESS;
    size_t n = 0, nsent = 0, nrecv = 0;
    if (!sendbuf || !recvbuf)
    {
        ier = MPI_ERR_BUFFER;
    }

    if (comm == MPI_COMM_NULL || root)
    {
        ier = MPI_ERR_COMM;
    }

    switch (sendtype)
    {
    case MPI_INT:
        n = sizeof(int);
        break;
    default:
        return MPI_ERR_TYPE;
    }
    nsent = n * sendcnt;

    switch (recvtype)
    {
    case MPI_INT:
        nrecv = sizeof(int);
        break;
    default:
        return MPI_ERR_TYPE;
    }
    nrecv = n * recvcnt;

    if (nrecv != nsent)
    {
        ier = MPI_ERR_COUNT;
    }

    if (ier == MPI_SUCCESS)
    {
        std::memcpy(recvbuf, sendbuf, nsent);
    }
    else
    {
        std::snprintf(mpierrmsg, MPI_MAX_ERROR_STRING,
                      "could not scatter data\n");
    }

    return ier;
}

int MPI_Scatterv(const void *sendbuf, const int *sendcnts, const int *displs,
                 MPI_Datatype sendtype, void *recvbuf, int recvcnt,
                 MPI_Datatype recvtype, int root, MPI_Comm comm)
{
    int ier = MPI_SUCCESS;
    if (!sendcnts || !displs)
    {
        ier = MPI_ERR_BUFFER;
    }
{'fraction_non_alphanumeric': 0.105929203539823, 'fraction_numerical': 0.006725663716814159, 'mean_word_length': 2.229779937124893, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4759220', 'n_tokens_mistral': 4205, 'n_tokens_neox': 3915, 'n_words': 1040}