/****************************************************************************
**  SIONLIB     http://www.fz-juelich.de/jsc/sionlib                       **
*****************************************************************************
**  Copyright (c) 2008-2019                                                **
**  Forschungszentrum Juelich, Juelich Supercomputing Centre               **
**                                                                         **
**  See the file COPYRIGHT in the package base directory for details       **
****************************************************************************/
/*
 * test_omp.c
 *
 *  Created on: Jun 29, 2010
 *      Author: dmontoya
 */

#define _XOPEN_SOURCE 700

#include <ctype.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <math.h>
#include <time.h>

#include "sion.h"
#include "sion_debug.h"
/* from configure */

#ifdef SION_OMP
#include <omp.h>
#endif

static int lock_counter = 0;
static int unlock_counter = 0;

#ifdef SION_PTHREADS
#include <pthread.h>
static pthread_mutex_t lock_data = PTHREAD_MUTEX_INITIALIZER;

int user_lock(void *data)
{
  int rc = SION_SUCCESS;
  if (pthread_mutex_lock(data)) {
    rc = SION_NOT_SUCCESS;
  };
  lock_counter++;
  DPRINTFP((1, "test_lock___1", -1, "lock___(%3d)\n", lock_counter));
  return rc;
}
int user_unlock(void *data)
{
  int rc = SION_SUCCESS;
  unlock_counter++;
  DPRINTFP((1, "test_lock___1", -1, "unlock_(%3d)\n", unlock_counter));
  if (pthread_mutex_unlock(data)) {
    rc = SION_NOT_SUCCESS;
  };
  return rc;
}
#else
static omp_lock_t lock_data;
int user_lock(void *data)
{
  int rc = SION_SUCCESS;
  omp_set_lock(&lock_data);
  lock_counter++;
  DPRINTFP((1, "test_lock___1", -1, "lock___(%3d)\n", lock_counter));
  return rc;
}
int user_unlock(void *data)
{
  int rc = SION_SUCCESS;
  unlock_counter++;
  DPRINTFP((1, "test_lock___1", -1, "unlock_(%3d)\n", unlock_counter));
  omp_unset_lock(&lock_data);
  return rc;
}
#endif

int main(int argc, char **argv)
{
#ifdef SION_OMP
  /* -------------------------- */
  /* PROLOG */
  /* -------------------------- */

#ifndef SION_PTHREADS
  omp_init_lock(&lock_data);
#endif

  _lsion_debug_set_query_thread_num_function(omp_get_thread_num);

#pragma omp parallel
  {
    int thread_num = omp_get_thread_num();
    int rc;

    DPRINTFP((1, "test_lock___1", thread_num, "start lock test\n"));

    fprintf(stderr, "on OMP thread %d: user callbacks defined: rc=%d\n", thread_num, sion_lock_user_callbacks_defined());
/* to avoid race conditions when counting lock events */
#pragma omp barrier
    rc = sion_lock_register_lock_callbacks(user_lock, user_unlock, &lock_data);
    fprintf(stderr, "                  lock_user_callbacks_defined returns rc=%d\n", rc);
#pragma omp barrier
    fprintf(stderr, "on OMP thread %d: user callbacks defined: rc=%d\n", thread_num, sion_lock_user_callbacks_defined());
  }
  printf("on OMP thread 0: (PROLOG) lock_counter  =%d \n", lock_counter);
  printf("on OMP thread 0: (PROLOG) unlock_counter=%d \n", unlock_counter);

  /* -------------------------- */
  /* TEST A: write a empty file */
  /* -------------------------- */

  sion_omp_options *options = sion_omp_options_new();
  if (!options) {
    return 1;
  }
  sion_omp_options_set_chunksize(options, 100);
  sion_omp_options_set_fsblksize(options, 10);

#pragma omp parallel
  {
    int sid = sion_paropen_omp("testA2.out", SION_OPEN_WRITE, options);
    int globalrank = -1;
    if (sid >= 0) {
      globalrank = sion_get_globalrank(sid);
      sion_parclose_omp(sid);
    }
    fflush(stderr);
    fflush(stdout);
    printf("on OMP thread %d: END of TEST A 2 files\n", globalrank);
  }
  printf("on OMP thread 0: (TEST A) lock_counter  =%d \n", lock_counter);
  printf("on OMP thread 0: (TEST A) unlock_counter=%d \n", unlock_counter);

  /* ------------------------------------------------------ */
  /* TEST B: write a small file with some different pattern */
  /* ------------------------------------------------------ */

#define BUFSIZE 1000
#define CHUNKSIZE 100

  sion_omp_options_set_chunksize(options, CHUNKSIZE);
  sion_omp_options_set_fsblksize(options, 16);

#pragma omp parallel
  {
    int thread_num = omp_get_thread_num();
    int sid = sion_paropen_omp("testB.out", SION_OPEN_WRITE, options);
    if (sid >= 0) {
      char buffer[BUFSIZE];
      /* test buffer which exact fits */
      for (int i = 0; i < BUFSIZE; i++) {
        buffer[i] = 'A' + thread_num * 3;
      }
      int b = 0;
      int rc = sion_ensure_free_space(sid, CHUNKSIZE);
      fprintf(stderr, "on OMP thread %d: ensure_free_space (%d) rc = %d\n", thread_num, CHUNKSIZE, rc);
      size_t bwrote = 0;
      if (rc) {
        bwrote = sion_fwrite(buffer, 1, CHUNKSIZE, sid);
      }
      int64_t bytes_written = sion_get_bytes_written(sid);
      printf("on OMP thread %d: bytes_written=%" PRId64 "\n", thread_num, bytes_written);
      long sum = 0;
      for (int i = 0; i < bwrote; i++) {
        sum = sum + buffer[i];
      }
      printf("on OMP thread %d: block=%d           bwrote=%3d blocksum=%8d \n", thread_num, b, (int)bwrote, (int)sum);

      /* test half size buffer */
      for (int i = 0; i < BUFSIZE; i++) {
        buffer[i] = 'A' + thread_num * 3 + 1;
      }
      b = 1;
      rc = sion_ensure_free_space(sid, CHUNKSIZE / 2);
      fprintf(stderr, "on OMP thread %d: ensure_free_space (%d) rc = %d\n", thread_num, CHUNKSIZE / 2, rc);
      if (rc) {
        bwrote = sion_fwrite(buffer, 1, CHUNKSIZE / 2, sid);
      }
      bytes_written = sion_get_bytes_written(sid);
      printf("on OMP thread %d: bytes_written=%" PRId64 "\n", thread_num, bytes_written);
      sum = 0;
      for (int i = 0; i < bwrote; i++) {
        sum = sum + buffer[i];
      }
      printf("on OMP thread %d: block=%d           bwrote=%3d blocksum=%8d \n", thread_num, b, (int)bwrote, (int)sum);

      /* test buffer which exact fits, leaving gap */
      for (int i = 0; i < BUFSIZE; i++) {
        buffer[i] = 'A' + thread_num * 3 + 2;
      }
      b = 2;
      rc = sion_ensure_free_space(sid, CHUNKSIZE);
      fprintf(stderr, "on OMP thread %d: ensure_free_space (%d) rc = %d\n", thread_num, CHUNKSIZE, rc);
      if (rc) {
        bwrote = sion_fwrite(buffer, 1, CHUNKSIZE, sid);
      }
      bytes_written = sion_get_bytes_written(sid);
      printf("on OMP thread %d: bytes_written=%" PRId64 "\n", thread_num, bytes_written);
      sum = 0;
      for (int i = 0; i < bwrote; i++) {
        sum = sum + buffer[i];
      }
      printf("on OMP thread %d: block=%d           bwrote=%3d blocksum=%8d \n", thread_num, b, (int)bwrote, (int)sum);

      sion_parclose_omp(sid);
    } else {
      fprintf(stderr, "on OMP thread %d: error sid = %d\n", thread_num, sid);

      printf("on OMP thread %d: END of TEST B write\n", thread_num);
    }
    fflush(stderr);
    fflush(stdout);
  }

  /* ------------------------------------------------------ */
  /* TEST B: read small file with some different pattern    */
  /* ------------------------------------------------------ */

#define BUFSIZE 1000
#define CHUNKSIZE 100

#pragma omp parallel
  {
    int thread_num = omp_get_thread_num();

    int sid = sion_paropen_omp("testB.out", SION_OPEN_READ, NULL);

    char buffer[BUFSIZE];
    for (int i = 0; i < BUFSIZE; i++) {
      buffer[i] = 'Q' + thread_num;
    }

    printf("on OMP thread %d: sid           =%d\n", thread_num, sid);

    if (sid >= 0) {
      printf("on OMP thread %d: chunksize     =%d\n", thread_num, (int)sion_get_chunksize(sid));
      printf("on OMP thread %d: thread_num    =%d\n", thread_num, thread_num);
      printf("on OMP thread %d: newfname      =%s\n", thread_num, sion_get_fname(sid));

      int b = 0;
      while ((!sion_feof(sid))) {
        int64_t bytes_avail = sion_bytes_avail_in_block(sid);
        printf("on OMP thread %d: block=%d           bytes_avail=%d\n", thread_num, b, (int)bytes_avail);
        size_t bytestoread = bytes_avail;
        size_t bread = sion_fread(buffer, 1, bytestoread, sid);
        long sum = 0;
        for (int i = 0; i < bytestoread; i++) {
          sum = sum + buffer[i];
        }
        printf("on OMP thread %d: block=%d           bread=%3d blocksum=%8d \n", thread_num, b, (int)bread, (int)sum);
        b++;
      }

      sion_parclose_omp(sid);
    } else {
      fprintf(stderr, "on OMP thread %d: error sid = %d\n", thread_num, sid);
    }

#pragma omp barrier
    printf("on OMP thread %d: END of TEST B read collective\n", thread_num);
  }

#pragma omp parallel
  {
    int thread_num = omp_get_thread_num();
    printf("on OMP thread %d: END of last OMP barrier\n", thread_num);
#pragma omp barrier
  }
  printf("on OMP thread 0: (END) lock_counter  =%d \n", lock_counter);
  printf("on OMP thread 0: (END) unlock_counter=%d \n", unlock_counter);

  fflush(stderr);
  fflush(stdout);

#endif
  return 0;
}
