text:
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.core.amqp.implementation;

import com.azure.core.amqp.exception.AmqpErrorCondition;
import com.azure.core.amqp.exception.AmqpErrorContext;
import com.azure.core.amqp.exception.AmqpException;
import com.azure.core.amqp.exception.AmqpResponseCode;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class ExceptionUtilTest {
    private final AmqpErrorContext context = new AmqpErrorContext("test-namespace");
    private final String message = "an-error-message";

    /**
     * Verifies correct exception is created from an error condition.
     */
    @Test
    public void createsCorrectException() {
        // Arrange
        AmqpErrorCondition condition = AmqpErrorCondition.ARGUMENT_OUT_OF_RANGE_ERROR;

        // Act
        Exception exception = ExceptionUtil.toException(condition.getErrorCondition(), message, context);

        // Assert
        Assertions.assertTrue(exception instanceof AmqpException);

        AmqpException amqpException = (AmqpException) exception;

        Assertions.assertEquals(condition, amqpException.getErrorCondition());
        Assertions.assertFalse(amqpException.isTransient());
        Assertions.assertSame(context, amqpException.getContext());
        Assertions.assertTrue(amqpException.getMessage().startsWith(message));
    }

    /**
     * Creates correct exception from not found exception that matches the expected service string.
     */
    @Test
    public void createsNotFoundException() {
        // Arrange
        AmqpResponseCode notFound = AmqpResponseCode.NOT_FOUND;
        AmqpErrorCondition condition = AmqpErrorCondition.NOT_FOUND;
        String entityName = "some-name";
        String message = "The messaging entity " + entityName + " could not be found";

        // Act
        Exception exception = ExceptionUtil.amqpResponseCodeToException(notFound.getValue(), message, context);

        // Assert
        Assertions.assertTrue(exception instanceof AmqpException);

        AmqpException amqpException = (AmqpException) exception;

        Assertions.assertEquals(condition, amqpException.getErrorCondition());
        Assertions.assertFalse(amqpException.isTransient());
        Assertions.assertSame(context, amqpException.getContext());
        Assertions.assertTrue(amqpException.getMessage().contains(message));
    }

    /**
     * Creates correct exception from not found exception.
     */
    @Test
    public void createsNotFoundExceptionNotMatches() {
        // Arrange
        AmqpResponseCode notFound = AmqpResponseCode.NOT_FOUND;
        AmqpErrorCondition condition = AmqpErrorCondition.NOT_FOUND;
        String message = "An entity was not found.";

        // Act
        Exception exception = ExceptionUtil.amqpResponseCodeToException(notFound.getValue(), message, context);

        // Assert
        Assertions.assertTrue(exception instanceof AmqpException);

        AmqpException amqpException = (AmqpException) exception;

        Assertions.assertEquals(condition, amqpException.getErrorCondition());
        Assertions.assertTrue(amqpException.isTransient());
        Assertions.assertSame(context, amqpException.getContext());
        Assertions.assertTrue(amqpException.getMessage().contains(message));
    }

    /**
     * Verifies AmqpException created from an integer.
     */
    @Test
    public void createsFromStatusCode() {
        // Arrange
        AmqpResponseCode responseCode = AmqpResponseCode.FORBIDDEN;
        AmqpErrorCondition actualCondition = AmqpErrorCondition.RESOURCE_LIMIT_EXCEEDED;

        // Act
        Exception exception = ExceptionUtil.amqpResponseCodeToException(responseCode.getValue(), message, context);

        // Assert
        Assertions.assertTrue(exception instanceof AmqpException);

        AmqpException amqpException = (AmqpException) exception;
        Assertions.assertEquals(actualCondition, amqpException.getErrorCondition());
        Assertions.assertFalse(amqpException.isTransient());
        Assertions.assertSame(context, amqpException.getContext());

        Assertions.assertTrue(amqpException.getMessage().contains(message));
    }

    /**
     * Verifies AmqpException created from a non-existent status code.
     */
    @Test
    public void createsFromNonExistentStatusCode() {
        // Arrange
        int unknownCode = 202;

        // Act
        Exception exception = ExceptionUtil.amqpResponseCodeToException(unknownCode, message, context);

        // Assert
        Assertions.assertTrue(exception instanceof AmqpException);

        AmqpException amqpException = (AmqpException) exception;
        Assertions.assertNull(amqpException.getErrorCondition());
        Assertions.assertTrue(amqpException.isTransient());
        Assertions.assertSame(context, amqpException.getContext());

        Assertions.assertTrue(amqpException.getMessage().contains(message));
    }
}


text:
package pv.com.pvcloudgo.model.bean;

import java.io.Serializable;

/**
 * Created by stefan on 17/1/4.
 */

public class Account implements Serializable {

    String whoclassName;
    String id;
    String whoId;
    String whoName;
    String withdrawalsPwd;
    float totalPrice;
    float frozenPrice;
    float availablePrice;
    String isQueren;
    String addressId;
    String addressTreeIds;

    public String getWhoclassName() {
        return whoclassName;
    }

    public String getId() {
        return id;
    }

    public String getWhoId() {
        return whoId;
    }

    public String getWhoName() {
        return whoName;
    }

    public String getWithdrawalsPwd() {
        return withdrawalsPwd;
    }

    public float getTotalPrice() {
        return totalPrice;
    }

    public float getFrozenPrice() {
        return frozenPrice;
    }

    public float getAvailablePrice() {
        return availablePrice;
    }

    public String getIsQueren() {
        return isQueren;
    }

    public String getAddressId() {
        return addressId;
    }

    public String getAddressTreeIds() {
        return addressTreeIds;
    }
}

redpajama_set_name: RedPajamaGithub


text:
        <ux-toolbar-search id="search"
            [alwaysExpanded]="alwaysExpanded"
            [(expanded)]="expanded"
            direction="right"
            background="white"
            (search)="onSearch($event)">

            <input
                uxToolbarSearchField
                #mastheadSearchField
                type="text"
                class="form-control"
                placeholder="Search"
                aria-label="Search"
                [(ngModel)]="searchText"
                [class.ux-toolbar-search-clear-offset]="searchText"
                (keydown.escape)="searchDropdownOpen = false"
                (blur)="searchDropdownOpen = false"
                (click)="searchDropdownOpen = true"/>

            <ux-typeahead
                #searchTypeahead
                class="typeahead-example"
                [(open)]="searchDropdownOpen"
                [filter]="searchText"
                [options]="options"
                (optionSelected)="searchText = $event.option">
            </ux-typeahead>

            <button *ngIf="searchText"
                    type="button"
                    class="btn btn-link btn-icon button-secondary ux-toolbar-search-clear"
                    (click)="searchText = ''; mastheadSearchField.focus()">
                <ux-icon name="close"></ux-icon>
            </button>

            <button uxToolbarSearchButton
                    type="button"
                    class="btn btn-link btn-icon button-secondary"
                    aria-label="Toggle Search">
                <ux-icon name="search"></ux-icon>
            </button>

        </ux-toolbar-search>

        <button type="button" class="btn btn-link btn-icon button-secondary">
            <ux-icon name="add"></ux-icon>
        </button>

        <button type="button" class="btn btn-link btn-icon button-secondary">
            <ux-icon name="edit"></ux-icon>
        </button>

Searched for:{{searchedFor}}


text:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
*	Name: Iplog Model
*
*	Version: 1.1
*
*	Author: Milos Milutinovic
*			milos.milutinovic@live.com
*
*	Created: 2015-07
*
*	Description: Handles logging and display of IP addresses and other access parameters.
*
*	Requires: Codeigniter 3
*/

class Iplog_model extends MY_Model {

	public function __construct(){
		parent::__construct();
	}

	//==================================================================
	//	Getters
	//==================================================================

	/**
	 * Returns data for all visits, paginated
	 *
	 * @returns Array
	 */
	public function getHits($order = 'date', $ordering = 'DESC', $start, $limit){
		$sql = "SELECT iplog_log.date, iplog_log.ip, iplog_log.referer, iplog_pages.uri, iplog_ua.ua, users.username FROM iplog_log LEFT JOIN iplog_pages ON iplog_log.page_id=iplog_pages.id LEFT JOIN iplog_ua ON iplog_log.ua=iplog_ua.id LEFT JOIN users ON iplog_log.user_id=users.id ORDER BY $order $ordering LIMIT $start, $limit";

		$query = $this->db->query($sql);

		return $query->result_array();
	}

	/**
	 * Returns page id
	 *
	 * @param string $uri Page URI
	 *
	 * @returns Object
	 */
	public function getPageId($uri){
		$this->db->where('uri', $uri);
		$query = $this->db->get('iplog_pages');

		$res = $query->row();

		if($res){
			return $res->id;
		}
		else{
			return FALSE;
		}
	}

	/**
	 * Returns User Agent ID
	 *
	 * @param string $ua User Agent string
	 *
	 * @returns Object
	 */
	public function getUaId($ua){
		$this->db->where('ua', $ua);
		$query = $this->db->get('iplog_ua');

		$res = $query->row();

		if($res){
			return $res->id;
		}
		else{
			return FALSE;
		}
	}

	/**
	 * Counts the number of page hits recorded in database
	 *
	 * @returns int
	 */
	public function countHits(){
		$sql = "SELECT COUNT(id) FROM iplog_log";
		$query = $this->db->query($sql);

		$result = $query->result_array();

		return $result[0]['COUNT(id)'];
	}

	/**
	 * Counts the number of page hits in several specific time periods
	 *
	 * @returns Array of integers
	 */
	public function countHitsByTime(){
		$now = time();
		$day = $now - 86400;
		$week = $now - 604800;
		$month = $now - 2592000;

		$sql = "SELECT COUNT(id) FROM iplog_log WHERE date>'$day'";
		$query = $this->db->query($sql);
		$res = $query->result_array();
		$result['day'] = $res[0]['COUNT(id)'];

		$sql = "SELECT COUNT(id) FROM iplog_log WHERE date>'$week'";
		$query = $this->db->query($sql);
		$res = $query->result_array();
		$result['week'] = $res[0]['COUNT(id)'];

		$sql = "SELECT COUNT(id) FROM iplog_log WHERE date>'$month'";
		$query = $this->db->query($sql);
		$res = $query->result_array();
		$result['month'] = $res[0]['COUNT(id)'];

		return $result;
	}

	//==================================================================
	//	Search methods
	//==================================================================

	/**
	 * Searches IPLOG tables
	 *
	 * @param string $type Type of search: ip|username|referer|uri
	 * @param string $search Search string
	 * @param int $start SQL start
	 * @param int $limit SQL limit
	 *
	 * @returns Array
	 */
	public function searchIplog($type, $search, $start, $limit){
		if($type == 'ip'){
			$sql = $this->queryBuilder('ip', $search);
		}
		else if($type == 'username'){
			$sql = $this->queryBuilder('username', $search);
		}
		else if($type == 'referer'){
			$sql = $this->queryBuilder('referer', $search);
		}
		else if($type == 'uri'){
			$sql = $this->queryBuilder('uri', $search);
		}

		$sql .= " LIMIT $start, $limit";
		$query = $this->db->query($sql);
		return $query->result_array();
	}

	/**
	 * Counts search results
	 *
	 * @param string $type Type of search: ip|username|referer|uri
	 * @param string $search Search string
	 *
	 * @returns int
	 */
	public function countSearch($type, $search){
		$query = $this->db->query($this->queryBuilder($type, $search));

		$result = $query->result_array();
		return count($result);
	}

	//==================================================================
	//	Insert functions
	//
	//	$data param is an array
	//==================================================================

	/**
	 * Inserts main log data
	 * @param array $data Array of data
	 *
	 * @returns bools $status
	 */
	public function insertLog($data){
		if($data['ip'] == null){
			$data['ip'] = 'n/a';
		}
		$status = $this->db->insert('iplog_log', $data);

		return $status;
	}

	/**
	 * Inserts new page into pages table
	 *
	 * @returns $status
	 */
	public function insertPage($data){
		if($data['uri'] == null){
			$data['uri'] = 'n/a';
		}

		$status = $this->db->insert('iplog_pages', $data);

		return $status;
	}

	/**
	 * Inserts new User Agent
	 *
	 * @returns $status
	 */
	public function insertUa($data){
		if($data['ua'] == null){
			$data['ua'] = 'n/a';
		}

		$status = $this->db->insert('iplog_ua', $data);

		return $status;
	}

	//==================================================================
	//	Private methods
	//==================================================================

	/**
	 * Builds queries for searching database
	 */
	private function queryBuilder($type, $search){
		if($type == 'ip'){
			$sql = "SELECT iplog_log.date, iplog_log.ip, iplog_log.referer, iplog_pages.uri, iplog_ua.ua, users.username FROM iplog_log LEFT JOIN iplog_pages ON iplog_log.page_id=iplog_pages.id LEFT JOIN iplog_ua ON iplog_log.ua=iplog_ua.id LEFT JOIN users ON iplog_log.user_id=users.id WHERE ip='$search' ORDER BY date ASC";
		}
		else if($type == 'username'){
			$sql = "SELECT iplog_log.date, iplog_log.ip, iplog_log.referer, iplog_pages.uri, iplog_ua.ua, users.username FROM iplog_log LEFT JOIN iplog_pages ON iplog_log.page_id=iplog_pages.id LEFT JOIN iplog_ua ON iplog_log.ua=iplog_ua.id LEFT JOIN users ON iplog_log.user_id=users.id WHERE users.username='$search' ORDER BY date ASC";
		}
		else if($type == 'referer'){
			$sql = "SELECT iplog_log.date, iplog_log.ip, iplog_log.referer, iplog_pages.uri, iplog_ua.ua, users.username FROM iplog_log LEFT JOIN iplog_pages ON iplog_log.page_id=iplog_pages.id LEFT JOIN iplog_ua ON iplog_log.ua=iplog_ua.id LEFT JOIN users ON iplog_log.user_id=users.id WHERE referer='$search' ORDER BY date ASC";
		}
		else if($type == 'uri'){
			$sql = "SELECT iplog_log.date, iplog_log.ip, iplog_log.referer, iplog_pages.uri, iplog_ua.ua, users.username FROM iplog_log LEFT JOIN iplog_pages ON iplog_log.page_id=iplog_pages.id LEFT JOIN iplog_ua ON iplog_log.ua=iplog_ua.id LEFT JOIN users ON iplog_log.user_id=users.id WHERE iplog_pages.uri='$search' ORDER BY date ASC";
		}
		return $sql;
	}

}


text:
const searchMaenoBunForShugo = (bunArr, bunNo, verbNo,bun,nodeArr)=>{
  //console.log("verbNo=%d",verbNo)

  //まずその分から探す
  for(let verbCnt = verbNo - 1 ; verbCnt >= 0 ; verbCnt-- ){
    const maenodoushi = bun.verbArr[verbCnt]
    if(maenodoushi.hasSubject){

      bun.verbArr[verbNo].rewriteSubjectAndAddBun2Node(nodeArr[maenodoushi.subject.nodeIdx], bun)

      return
    }
  }

  //次にそれより前の文から探す
  for(let bunCnt = bunNo - 1 ; bunCnt >= 0 ; bunCnt-- ){
    if(Object.keys(bunArr[bunCnt]).length!==0 && bunArr[bunCnt].verbArr!==void 0){
      for(let verbCnt=bunArr[bunCnt].verbArr.length - 1; verbCnt >= 0 ; verbCnt--){
        const maenodoushi = bunArr[bunCnt].verbArr[verbCnt]
        if(maenodoushi.hasSubject){
          bun.verbArr[verbNo].rewriteSubjectAndAddBun2Node(nodeArr[maenodoushi.subject.nodeIdx], bun)
          return
        }
      }
    }
  }
}

export {searchMaenoBunForShugo}


text:
import { IQEntity, IEntityRelationFrom } from "./Entity";
import { IQRelation, EntityRelationType } from "./Relation";
/**
 * Created by Papa on 10/25/2016.
 */
export declare class QOneToManyRelation<IERF extends IEntityRelationFrom, R> implements IQRelation {
    q: IQEntity;
    qConstructor: new () => IQEntity;
    entityName: string;
    propertyName: string;
    relationEntityConstructor: new () => R;
    relationQEntityConstructor: new (...args: any[]) => IQEntity;
    relationType: EntityRelationType;
    constructor(q: IQEntity, qConstructor: new () => IQEntity, entityName: string, propertyName: string, relationEntityConstructor: new () => R, relationQEntityConstructor: new (...args: any[]) => IQEntity);
    innerJoin(): IERF;
    leftJoin(): IERF;
    private getNewQEntity(joinType);
}


text:
#include <omp.h>
#include <sstream>
#include <stdexcept>
#include <unordered_map>

#include "cudautil.h"
#include "KnnData.h"
#include "MathUtil.h"

namespace
{
static const int ROW_PADDING = 8;
static const std::unordered_map<std::string, astdl::knn::DataType> STRING_TO_DATA_TYPE = { { "fp32",
    astdl::knn::DataType::FP32 }, { "fp16", astdl::knn::DataType::FP16 }, };
}  // namespace

namespace astdl
{
namespace knn
{

Matrix::Matrix() :
    data(nullptr),
    numRows(0),
    numColumns(0),
    elementSize(0),
    memoryType(cudaMemoryTypeHost)
{
}

Matrix::Matrix(void *data, uint32_t numRows, int numColumns, size_t elementSize, cudaMemoryType memoryType) :
    data(data),
    numRows(numRows),
    numColumns(numColumns),
    elementSize(elementSize),
    memoryType(memoryType)
{
}

size_t Matrix::getSizeInBytes()
{
    size_t sizeInBytes = numRows * numColumns * elementSize;
    return sizeInBytes;
}

size_t Matrix::getLength()
{
    size_t length = numRows * numColumns;
    return length;
}

std::string getDataTypeString(DataType dataType)
{
    switch (dataType) {
        case DataType::FP32:
            return "fp32";
        case DataType::FP16:
            return "fp16";
        default:
            return "unknown";
    }
}

DataType getDataTypeFromString(const std::string &dataTypeLiteral)
{
    auto entry = STRING_TO_DATA_TYPE.find(dataTypeLiteral);
    if (entry == STRING_TO_DATA_TYPE.end())
    {
        std::stringstream msg;
        msg << "Unknown DataType " << dataTypeLiteral;
        throw std::invalid_argument(msg.str());
    }
    return entry->second;
}

Matrix loadDataOnHost(DataReader *dataReader)
{
    uint32_t rows = dataReader->getRows();
    int columns = dataReader->getColumns();
    size_t elementSize = sizeof(float);

    Matrix matrix = allocateMatrixOnHost(rows, columns, elementSize);

    std::string ignored;
    for (int rowNum = 0; dataReader->readRow(&ignored, ((float*) matrix.data) + (rowNum * columns)); ++rowNum)
    {
    }
    return matrix;
}

KnnData::KnnData(int numGpus, int batchSize, int maxK, DataType dataType) :
    numGpus(numGpus),
    batchSize(batchSize),
    maxK(maxK),
    dataType(dataType),
    dCollectionPartitions(numGpus),
    dInputBatches(numGpus),
    dProducts(numGpus),
    dResultScores(numGpus),
    dResultIndexes(numGpus),
    hResultScores(numGpus),
    hResultIndexes(numGpus),
    dInputBatchTmpBuffers(numGpus),
    collectionRowsPadded(numGpus),
    hKeys(numGpus),
    elapsedSgemm(numGpus),
    elapsedTopK(numGpus)
{
// sanity check number of GPUs
    int deviceCount = astdl::cuda_util::getDeviceCount();
    if (deviceCount < 1)
    {
        std::stringstream msg;
        msg << "No GPU device found on host. Device count is " << deviceCount;
        throw std::runtime_error(msg.str());
    }

    if (deviceCount < numGpus)
    {
        std::stringstream msg;
        msg << "Not enough GPUs on host. Required " << numGpus << ", found " << deviceCount;
        throw std::runtime_error(msg.str());
    }

    fprintf(stderr, "INFO: Initializing KnnData with numGpus = %d, batchSize = %d, maxK = %d, dataType = %s\n", numGpus,
            batchSize, maxK, getDataTypeString(dataType).c_str());
// fp16 mode only supported on sm >= 7
    if (dataType == DataType::FP16)
    {
        cudaDeviceProp deviceProp;
        CHECK_ERR(cudaGetDeviceProperties(&deviceProp, 0));
        int smMajor = deviceProp.major;
        int smMinor = deviceProp.minor;

        if (smMajor < 7)
        {
            fprintf(stderr, "WARNING: fp16 compute is not supported in sm %d.%d < 7. Only storing data in fp16.\n",
                    smMajor, smMinor);
        }
    }

    for (int i = 0; i < numGpus; ++i)
    {
        CHECK_ERR(cudaSetDevice(i));
        cublasHandle_t handle;
        STATUS_ERR(cublasCreate(&handle));

        if (dataType == DataType::FP16)
        {
            fprintf(stderr, "INFO: On device %d, setting cublas mode to CUBLAS_TENSOR_OP_MATH\n", i);
            STATUS_ERR(cublasSetMathMode(handle, CUBLAS_TENSOR_OP_MATH));
        }

        cublasHandles.push_back(handle);
    }
}

int KnnData::getFeatureSize() const
{
    return dCollectionPartitions[0].numColumns;
}

void KnnData::load(int device, DataReader *dataReader)
{
    CHECK_ERR(cudaSetDevice(device));

// pad by multiples of 4 for sgemm
    uint32_t actualRows = dataReader->getRows();
    uint32_t rows = ((actualRows + (ROW_PADDING - 1)) / ROW_PADDING) * ROW_PADDING;
    uint32_t rowsPadded = rows - actualRows;
    int columns = dataReader->getColumns();

// holds the data on host memory until we copy it over to the device
    Matrix hTmpMatrix = allocateMatrixOnHost(rows, columns, sizeof(float));
    size_t hTmpDataBytes = hTmpMatrix.getSizeInBytes();
    float *hTmpData = (float*) hTmpMatrix.data;

    collectionRowsPadded[device] = rowsPadded;

    std::string key;
    float vector[columns];
//    for (int rowNum = 0; dataReader->readRow(&key, hTmpData + (rowNum * columns)); ++rowNum)
    for (int rowNum = 0; dataReader->readRow(&key, vector); ++rowNum)
    {
        hKeys[device].push_back(key);
        // copy vector into hTmpData in column major format
        for(int j = 0; j < columns; ++j) {
            hTmpData[j * rows + rowNum] = vector[j];
        }
    }

    if (dataType == DataType::FP16)
    {
        dCollectionPartitions[device] = allocateMatrixOnDevice(rows, columns, sizeof(half));
        astdl::math::kFloatToHalf(hTmpData, hTmpDataBytes, (half*) dCollectionPartitions[device].data);

        dInputBatches[device] = allocateMatrixOnDevice(batchSize, columns, sizeof(half));
        dInputBatchTmpBuffers[device] = allocateMatrixOnDevice(batchSize, columns, sizeof(float));
    } else
    {
        dCollectionPartitions[device] = allocateMatrixOnDevice(rows, columns, sizeof(float));
        CHECK_ERR(cudaMemcpy(dCollectionPartitions[device].data, hTmpData, hTmpDataBytes, cudaMemcpyHostToDevice));
        dInputBatches[device] = allocateMatrixOnDevice(batchSize, columns, sizeof(float));
    }

    free(hTmpData);

    dProducts[device] = allocateMatrixOnDevice(batchSize, rows, sizeof(float));
    dResultScores[device] = allocateMatrixOnDevice(batchSize, maxK, sizeof(float));
    dResultIndexes[device] = allocateMatrixOnDevice(batchSize, maxK, sizeof(uint32_t));

    hResultScores[device] = allocateMatrixOnHost(batchSize, maxK, sizeof(float));
    hResultIndexes[device] = allocateMatrixOnHost(batchSize, maxK, sizeof(uint32_t));

    size_t totalMemory;
    size_t freeMemory;
    astdl::cuda_util::getDeviceMemoryInfoInMb(device, &totalMemory, &freeMemory);

    fprintf(
        stderr,
        "INFO: loaded %zu (%zu padded) rows and %d columns into device %d. Used: %zu MB, Free: %zu MB, Total: %zu MB\n",
        actualRows, rows, columns, device, totalMemory - freeMemory, freeMemory, totalMemory);
}

void KnnData::load(const std::map<int, DataReader*> &deviceToData)
{
    omp_set_num_threads(numGpus);
#pragma omp parallel
    {
        int device = omp_get_thread_num();
        auto dataReader = deviceToData.find(device);
        if (dataReader == deviceToData.end())
        {
            std::stringstream msg;
            msg << "Data reader for device " << device << " not specified. Must specify readers for all " << numGpus
                << " devices";
            throw std::runtime_error(msg.str());
        }

        load(device, dataReader->second);
    }
}

void KnnData::load(const std::map<int, std::string> &deviceToFile, char keyValDelim, char vecDelim)
{
    std::map<int, DataReader*> deviceToData;
    for (const auto &entry : deviceToFile)
    {
        int device = entry.first;
        std::string file = entry.second;
        DataReader *dataReader = new TextFileDataReader(file, keyValDelim, vecDelim);
        deviceToData.insert( { device, dataReader });
    }

    load(deviceToData);

    for (const auto &entry : deviceToData)
    {
        delete entry.second;
    }
    deviceToData.clear();
}

KnnData::~KnnData()
{
    for (auto handle : cublasHandles)
    {
        cublasDestroy(handle);
    }
    for (auto dCollection : dCollectionPartitions)
    {
        freeMatrix(dCollection);
    }
    for (auto dInputBatch : dInputBatches)
    {
        freeMatrix(dInputBatch);
    }
    for (auto dProduct : dProducts)
    {
        freeMatrix(dProduct);
    }
    for (auto dResultScore : dResultScores)
    {
        freeMatrix(dResultScore);
    }
    for (auto dResultIndex : dResultIndexes)
    {
        freeMatrix(dResultIndex);
    }
    for (auto hResultScore : hResultScores)
    {
        freeMatrix(hResultScore);
    }
    for (auto hResultIndex : hResultIndexes)
    {
        freeMatrix(hResultIndex);
    }
    for (auto hKey : hKeys)
    {
        hKey.clear();
    }

    for (auto dInputBatchTmpBuffer : dInputBatchTmpBuffers)
    {
        freeMatrix(dInputBatchTmpBuffer);
    }

    cublasHandles.clear();
    dCollectionPartitions.clear();
    dInputBatches.clear();
    dProducts.clear();
    dResultScores.clear();
    dResultIndexes.clear();
    hKeys.clear();
    elapsedSgemm.clear();
    elapsedTopK.clear();
}

Matrix allocateMatrixOnHost(uint32_t numRows, int numColumns, size_t elementSize)
{
    void *data = malloc(numRows * numColumns * elementSize);
    return Matrix(data, numRows, numColumns, elementSize, cudaMemoryTypeHost);
}

Matrix allocateMatrixOnDevice(uint32_t numRows, int numColumns, size_t elementSize)
{
    void *data;
    CHECK_ERR(cudaMalloc(&data, numRows * numColumns * elementSize));
    return Matrix(data, numRows, numColumns, elementSize, cudaMemoryTypeDevice);
}

void freeMatrix(const Matrix &matrix)
{
    if (matrix.data != nullptr)
    {
        switch (matrix.memoryType) {
            case cudaMemoryTypeDevice:
                CHECK_ERR(cudaFree(matrix.data))
                break;
            case cudaMemoryTypeHost:
                free(matrix.data);
                break;
            default:
                std::stringstream msg;
                msg << "Unknown memory type " << matrix.memoryType;
                throw std::invalid_argument(msg.str());
        }
    }
}
}  // namespace knn
}  // namespace astdl


text:
canvas {
   display : block;
   margin : auto;
}