<commit_before>/**
Copyright (c) 2015, Intel Corporation. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the Intel Corporation nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL INTEL CORPORATION BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <cstdio>
#include <cstring>
#include <climits>
#include <algorithm>

#include <omp.h>

#include "CSR.hpp"
#include "COO.hpp"
#include "mm_io.h"
#include "Utils.hpp"
#include "MemoryPool.hpp"

#ifdef LOADIMBA
#include "synk/loadimba.hpp"
#else
#include "synk/barrier.hpp"
#endif

using namespace std;

#ifdef LOADIMBA
extern synk::LoadImba *bar;
#else
extern synk::Barrier *bar;
#endif

namespace SpMP
{

bool CSR::useMemoryPool_() const
{
  return MemoryPool::getSingleton()->contains(rowptr);
}

CSR::CSR() : rowptr(NULL), colidx(NULL), values(NULL), idiag(NULL), diag(NULL), diagptr(NULL), extptr(NULL), base(0), ownData_(false)
{
}

void CSR::alloc(int m, int nnz, bool createSeparateDiagData /*= true*/)
{
  this->m = m;

  rowptr = MALLOC(int, m + 1);
  colidx = MALLOC(int, nnz);
  values = MALLOC(double, nnz);
  diagptr = MALLOC(int, m);

  assert(rowptr != NULL);
  assert(colidx != NULL);
  assert(values != NULL);
  assert(diagptr != NULL);

  if (createSeparateDiagData) {
    idiag = MALLOC(double, m);
    diag = MALLOC(double, m);
    assert(idiag != NULL);
    assert(diag != NULL);
  }

  ownData_ = true;
}

CSR::CSR(int m, int n, int nnz, int base /*=0*/)
 : base(base)
{
  this->m=m;
  this->n=n;
  alloc(n, nnz);
}

CSR::CSR(const char *file, bool forceSymmetric /*=false*/, int pad /*=1*/)
 : base(0), rowptr(NULL), colidx(NULL), values(NULL), idiag(NULL), diag(NULL), diagptr(NULL), extptr(NULL)
{
  int m = atoi(file);
  char buf[1024];
  sprintf(buf, "%d", m);

  int l = strlen(file);

  if (!strcmp(buf, file)) {
    generate3D27PtLaplacian(this, m);
  }
  else {
    COO Acoo;
    load_matrix_market((char *)file, Acoo, forceSymmetric, pad);

    alloc(Acoo.m, Acoo.nnz);

    dcoo2crs(&Acoo, this);
  }
}

CSR::CSR(int m, int n, int *rowptr, int *colidx, double *values, int base /*=0*/) :
 m(m), n(n), rowptr(rowptr), colidx(colidx), values(values), ownData_(false), idiag(NULL), diag(NULL), extptr(NULL)
{
  diagptr = MALLOC(int, m);
#pragma omp parallel for
  for (int i = 0; i < m; ++i) {
    for (int j = rowptr[i] - base; j < rowptr[i + 1] - base; ++j) {
      if (colidx[j] - base == i) {
        diagptr[i] = j + base;
      }
    }
  }
}

void CSR::dealloc()
{
  if (useMemoryPool_()) {
    // a large single contiguous chunk is allocated to
    // buffers except rowptr and colidx.
    rowptr = NULL;
    extptr = NULL;
    colidx = NULL;
    values = NULL;
    idiag = NULL;
    diag = NULL;
    diagptr = NULL;
  }
  else {
    if (ownData_) {
      FREE(rowptr);
      FREE(extptr);
      FREE(colidx);
      FREE(values);
    }

    FREE(idiag);
    FREE(diag);
    FREE(diagptr);
  }
}

CSR::~CSR()
{
  dealloc();
}

bool CSR::isSymmetric(bool checkValues /*=true*/, bool printFirstNonSymmetry /* = false*/) const
{
  const int *extptr = this->extptr ? this->extptr : rowptr + 1;
  for (int i = 0; i < m; ++i) {
    for (int j = rowptr[i] - base; j < extptr[i] - base; ++j) {
      int c = colidx[j] - base;
      if (c > i) {
        bool hasPair = false;
        for (int k = rowptr[c] - base; k < extptr[c] - base; ++k) {
          if (colidx[k] - base == i) {
            hasPair = true;
            if (checkValues && values[j] != values[k]) {
              if (printFirstNonSymmetry) {
                printf(
                  "assymmetric (%d, %d) = %g, (%d, %d) = %g\n", 
                  i + 1, c + 1, values[j], c + 1, i + 1, values[k]);
              }
              return false;
            }
            break;
          }
        }
        if (!hasPair) {
          if (printFirstNonSymmetry) {
            printf(
              "assymmetric (%d, %d) exists but (%d, %d) doesn't\n",
              i + 1, c + 1, c + 1, i + 1);
          }
          return false;
        }
      }
    }
  } // for each row

  return true;
}
<commit_msg>initialize base<commit_after>/**
Copyright (c) 2015, Intel Corporation. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the Intel Corporation nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL INTEL CORPORATION BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <cstdio>
#include <cstring>
#include <climits>
#include <algorithm>

#include <omp.h>

#include "CSR.hpp"
#include "COO.hpp"
#include "mm_io.h"
#include "Utils.hpp"
#include "MemoryPool.hpp"

#ifdef LOADIMBA
#include "synk/loadimba.hpp"
#else
#include "synk/barrier.hpp"
#endif

using namespace std;

#ifdef LOADIMBA
extern synk::LoadImba *bar;
#else
extern synk::Barrier *bar;
#endif

namespace SpMP
{

bool CSR::useMemoryPool_() const
{
  return MemoryPool::getSingleton()->contains(rowptr);
}

CSR::CSR() : rowptr(NULL), colidx(NULL), values(NULL), idiag(NULL), diag(NULL), diagptr(NULL), extptr(NULL), base(0), ownData_(false)
{
}

void CSR::alloc(int m, int nnz, bool createSeparateDiagData /*= true*/)
{
  this->m = m;

  rowptr = MALLOC(int, m + 1);
  colidx = MALLOC(int, nnz);
  values = MALLOC(double, nnz);
  diagptr = MALLOC(int, m);

  assert(rowptr != NULL);
  assert(colidx != NULL);
  assert(values != NULL);
  assert(diagptr != NULL);

  if (createSeparateDiagData) {
    idiag = MALLOC(double, m);
    diag = MALLOC(double, m);
    assert(idiag != NULL);
    assert(diag != NULL);
  }

  ownData_ = true;
}

CSR::CSR(int m, int n, int nnz, int base /*=0*/)
 : base(base)
{
  this->m=m;
  this->n=n;
  alloc(n, nnz);
}

CSR::CSR(const char *file, bool forceSymmetric /*=false*/, int pad /*=1*/)
 : base(0), rowptr(NULL), colidx(NULL), values(NULL), idiag(NULL), diag(NULL), diagptr(NULL), extptr(NULL)
{
  int m = atoi(file);
  char buf[1024];
  sprintf(buf, "%d", m);

  int l = strlen(file);

  if (!strcmp(buf, file)) {
    generate3D27PtLaplacian(this, m);
  }
  else {
    COO Acoo;
    load_matrix_market((char *)file, Acoo, forceSymmetric, pad);

    alloc(Acoo.m, Acoo.nnz);

    dcoo2crs(&Acoo, this);
  }
}

CSR::CSR(int m, int n, int *rowptr, int *colidx, double *values, int base /*=0*/) :
 m(m), n(n), rowptr(rowptr), colidx(colidx), values(values), ownData_(false), idiag(NULL), diag(NULL), extptr(NULL), base(base)
{
  diagptr = MALLOC(int, m);
#pragma omp parallel for
  for (int i = 0; i < m; ++i) {
    for (int j = rowptr[i] - base; j < rowptr[i + 1] - base; ++j) {
      if (colidx[j] - base == i) {
        diagptr[i] = j + base;
      }
    }
  }
}

void CSR::dealloc()
{
  if (useMemoryPool_()) {
    // a large single contiguous chunk is allocated to
    // buffers except rowptr and colidx.
    rowptr = NULL;
    extptr = NULL;
    colidx = NULL;
    values = NULL;
    idiag = NULL;
    diag = NULL;
    diagptr = NULL;
  }
  else {
    if (ownData_) {
      FREE(rowptr);
      FREE(extptr);
      FREE(colidx);
      FREE(values);
    }

    FREE(idiag);
    FREE(diag);
    FREE(diagptr);
  }
}

CSR::~CSR()
{
  dealloc();
}

bool CSR::isSymmetric(bool checkValues /*=true*/, bool printFirstNonSymmetry /* = false*/) const
{
  const int *extptr = this->extptr ? this->extptr : rowptr + 1;
  for (int i = 0; i < m; ++i) {
    for (int j = rowptr[i] - base; j < extptr[i] - base; ++j) {
      int c = colidx[j] - base;
      if (c != i) {
        bool hasPair = false;
        for (int k = rowptr[c] - base; k < extptr[c] - base; ++k) {
          if (colidx[k] - base == i) {
            hasPair = true;
            if (checkValues && values[j] != values[k]) {
              if (printFirstNonSymmetry) {
                printf(
                  "assymmetric (%d, %d) = %g, (%d, %d) = %g\n", 
                  i + 1, c + 1, values[j], c + 1, i + 1, values[k]);
              }
              return false;
            }
            break;
          }
        }
        if (!hasPair) {
          if (printFirstNonSymmetry) {
            printf(
              "assymmetric (%d, %d) exists but (%d, %d) doesn't\n",
              i + 1, c + 1, c + 1, i + 1);
          }
          return false;
        }
      }
    }
  } // for each row

  return true;
}

{'fraction_non_alphanumeric': 0.13423195731536086, 'fraction_numerical': 0.005616399887672002, 'mean_word_length': 2.8893338187113216, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 26, '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': '3117117', 'n_tokens_mistral': 3786, 'n_tokens_neox': 3581, 'n_words': 1178}