<commit_before>#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
#include <time.h>
#include <string.h>
#include <unistd.h>

#include <mpi.h>
#include "mpsort.h"

static void radix_int(const void * ptr, void * radix, void * arg) {
    *(int64_t*)radix = *(const int64_t*) ptr + INT64_MIN;
}
static int64_t
checksum(int64_t * data, size_t localsize, MPI_Comm comm)
{
    int64_t sum = 0;
    ptrdiff_t i;
    for(i = 0; i < localsize; i ++) {
        sum += data[i];
    }
    
    MPI_Allreduce(MPI_IN_PLACE, &sum, 1, MPI_LONG, MPI_SUM, comm);
    return sum;
}
static void
generate(int64_t * data, size_t localsize, int bits, int seed)
{
    /* only keep bits of precision. */
    srandom(seed);

    ptrdiff_t i;
    unsigned shift = 64u - bits;
    for(i = 0; i < localsize; i ++) {
        uint64_t value = (int64_t) random() * (int64_t) random() * random() * random();
        data[i] = (signed) ((value << shift));
    }
}

static void
check_sorted(int64_t * data, size_t localsize, MPI_Comm comm)
{
    ptrdiff_t i;
    int ThisTask, NTask;
    MPI_Comm_rank(comm, &ThisTask);
    MPI_Comm_size(comm, &NTask);

    const int TAG = 0xbeef;

    ptrdiff_t dummy[1] = {INT64_MIN};

    for(i = 1; i < localsize; i ++) {
        if(data[i] < data[i - 1]) {
            fprintf(stderr, "Ordering of local array is broken. \n");
            MPI_Abort(comm, -1);
        }
    }

    if(NTask == 1) return;


    int64_t prev = -1;

    while(1) {
        if(ThisTask == 0) {
            int64_t * ptr = dummy;
            if(localsize > 0) {
                ptr = &data[localsize - 1];
            }
            MPI_Send(ptr, 1, MPI_LONG, ThisTask + 1, TAG, comm);
            break;
        }
        if(ThisTask == NTask - 1) {
            MPI_Recv(&prev, 1, MPI_LONG,
                    ThisTask - 1, TAG, comm, MPI_STATUS_IGNORE);
            break;
        }
        /* else */
        if(localsize == 0) {
            /* simply pass through whatever we get */
            MPI_Recv(&prev, 1, MPI_LONG, ThisTask - 1, TAG, comm, MPI_STATUS_IGNORE);
            MPI_Send(&prev, 1, MPI_LONG, ThisTask + 1, TAG, comm);
            break;
        }
        /* else */
        { 
            MPI_Sendrecv(
                    &data[localsize - 1], 1, MPI_LONG, 
                    ThisTask + 1, TAG, 
                    &prev, 1, MPI_LONG,
                    ThisTask - 1, TAG, comm, MPI_STATUS_IGNORE);
            break;
        }
    }

    if(ThisTask > 1) {
        if(localsize > 0) {
//                printf("ThisTask = %d prev = %d\n", ThisTask, prev);
            if(prev > data[0]) {
                fprintf(stderr, "global ordering fail\n");
                MPI_Abort(comm, -1);
            }
        }
    }
}
static void usage()
{
        printf("./main [number of items]\n");
}

int main(int argc, char * argv[]) {
    MPI_Init(&argc, &argv);

    int ThisTask;
    int NTask;

    MPI_Comm_size(MPI_COMM_WORLD, &NTask);
    MPI_Comm_rank(MPI_COMM_WORLD, &ThisTask);

    extern char * optarg;<commit_msg>Fix invalid memory access causing tests to fail<commit_after>#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
#include <time.h>
#include <string.h>
#include <unistd.h>

#include <mpi.h>
#include "mpsort.h"

static void radix_int(const void * ptr, void * radix, void * arg) {
    *(int64_t*)radix = *(const int64_t*) ptr + INT64_MIN;
}
static int64_t
checksum(int64_t * data, size_t localsize, MPI_Comm comm)
{
    int64_t sum = 0;
    ptrdiff_t i;
    for(i = 0; i < localsize; i ++) {
        sum += data[i];
    }
    
    MPI_Allreduce(MPI_IN_PLACE, &sum, 1, MPI_LONG, MPI_SUM, comm);
    return sum;
}
static void
generate(int64_t * data, size_t localsize, int bits, int seed)
{
    /* only keep bits of precision. */
    srandom(seed);

    ptrdiff_t i;
    unsigned shift = 64u - bits;
    for(i = 0; i < localsize; i ++) {
        uint64_t value = (int64_t) random() * (int64_t) random() * random() * random();
        data[i] = (signed) ((value << shift));
    }
}

static void
check_sorted(int64_t * data, size_t localsize, MPI_Comm comm)
{
    ptrdiff_t i;
    int ThisTask, NTask;
    MPI_Comm_rank(comm, &ThisTask);
    MPI_Comm_size(comm, &NTask);

    const int TAG = 0xbeef;

    ptrdiff_t dummy[1] = {INT64_MIN};

    for(i = 1; i < localsize; i ++) {
        if(data[i] < data[i - 1]) {
            fprintf(stderr, "Ordering of local array is broken. \n");
            MPI_Abort(comm, -1);
        }
    }

    if(NTask == 1) return;


    int64_t prev = -1;

    while(1) {
        if(ThisTask == 0) {
            int64_t * ptr = dummy;
            if(localsize > 0) {
                ptr = &data[localsize - 1];
            }
            MPI_Send(ptr, 1, MPI_LONG, ThisTask + 1, TAG, comm);
            break;
        }
        if(ThisTask == NTask - 1) {
            MPI_Recv(&prev, 1, MPI_LONG,
                    ThisTask - 1, TAG, comm, MPI_STATUS_IGNORE);
            break;
        }
        /* else */
        if(localsize == 0) {
            /* simply pass through whatever we get */
            MPI_Recv(&prev, 1, MPI_LONG, ThisTask - 1, TAG, comm, MPI_STATUS_IGNORE);
            MPI_Send(&prev, 1, MPI_LONG, ThisTask + 1, TAG, comm);
            break;
        }
        else
        {
            MPI_Sendrecv(
                    &data[localsize - 1], 1, MPI_LONG, 
                    ThisTask + 1, TAG, 
                    &prev, 1, MPI_LONG,
                    ThisTask - 1, TAG, comm, MPI_STATUS_IGNORE);
            break;
        }
    }

    if(ThisTask > 1) {
        if(localsize > 0) {
//                printf("ThisTask = %d prev = %d\n", ThisTask, prev);
            if(prev > data[0]) {
                fprintf(stderr, "Task %d global ordering fail: %ld > %ld\n", ThisTask, prev, data[0]);
                MPI_Abort(comm, -1);
            }
        }
    }
}
static void usage()
{
        printf("./main [number of items]\n");
}

int main(int argc, char * argv[]) {
    MPI_Init(&argc, &argv);

    int ThisTask;
    int NTask;

    MPI_Comm_size(MPI_COMM_WORLD, &NTask);
    MPI_Comm_rank(MPI_COMM_WORLD, &ThisTask);

    extern char * optarg;
{'fraction_non_alphanumeric': 0.1380204114693018, 'fraction_numerical': 0.02089745666612668, 'mean_word_length': 1.8702928870292888, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 28, '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': '2424245', 'n_tokens_mistral': 2397, 'n_tokens_neox': 2208, 'n_words': 541}