Wrong values with using gpu.js

604 views Asked by At

I use my code in modes 'dev', 'gpu' and 'cpu'. When I run up my code in 'dev' mode I had right number of results word 'more' in almost 100mb file. There is 259200 words 'more'. But when I run it up in 'gpu' or 'cpu' mode, I have only 3 results from entire file. And second thing, even if I used 'gpu' and 'cpu' mode there is a time differences. When I used gpu it finish in 4000-4100ms and when I use cpu it finish in 2600-2700ms. I think using gpu should be faster than cpu in this searching. Hope for help!

const { GPU } = require('gpu.js');
const fs = require('fs');
const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

const gpu = new GPU({ mode: 'cpu' });

rl.question('Wprowadż szukaną frazę: ', (value) => {
  let phrase = value;
  let start = new Date();

  fs.readFile('plik2.txt', 'utf8', function(err, data) {
    if (err) throw err;
    let tempPhraseLen = phrase.length;
    let tempDataLen = data.length;
    let tempPhrase = [];
    for (let i = 0; i < tempPhraseLen; i++) {
      tempPhrase.push(phrase[i].charCodeAt(0));
    }
    let tempData = [];
    for (let i = 0; i < tempDataLen; i++) {
      tempData.push(data[i].charCodeAt(0));
    }

    function kernelFunction(pFunc, arrFunc, pLen, arrLen) {
      let done = false;
      let counter = 0;
      for (let i = 0; i < arrLen; i++) {
        if (arrFunc[i] === pFunc[0]) {
          let isChosen = true;
          let k = 0;
          for (let j = i; j < i + pLen; j++) {
            isChosen = true;
            if (arrFunc[j] !== pFunc[k]) {
              isChosen = false;
              break;
            }
            k++;
          }
          if (isChosen) {
            done = true;
            counter++;
          }
        }
      }
      if (!done) {
        return 0;
      } else {
        return counter;
      }
    }
    const kernel = gpu.createKernel(kernelFunction, {
      output: [ 1 ],
      tactic: 'precision'
    });

    const result = kernel(tempPhrase, tempData, tempPhraseLen, tempDataLen);

    if (result[0] === 0) {
      console.log('Nie udało się znaleźć podanego wzorca');
    } else {
      console.log('Znalazłeś ' + result[0] + ' powtórzeń/nia frazy "' + phrase + '"');
    }
    let end = new Date() - start;
    console.info('Time: %dms', end);
  });
  rl.close();
});

0

There are 0 answers