mardi 2 mai 2023

c++ how to perform efficient dot-product between two vectors of uint64?

Given two same size vectors of uint64. I simply wants to compute the dot-product between these vectors. As it will require 128 bit arithmetic to store the intermediate results. I come up with a simple function

void multiply_acum(uint64_t op1, uint64_t op2, uint128_t& product_acum) {
     product_acum =  product_acum + (static_cast<uint128_t>(op1) * static_cast<uint128_t>(op2)); }

and call this function from dot-product function (may not be working providing just for reference)

     void dot_product(int size, vector<uint64> a, vector<uint64> b){
       uint128_t product_acum = 0;
       for (int i = 0; i < size; i++){
        multiply_acum(a[i], b[i], product_acum);
      }}

This function is working fine but 128 bit arithmetic is quite slow. Any suggestions on improving the performance of if could avoid 128 bits arithmetics and use 64 bits instead as explained here Getting the high part of 64 bit integer multiplication

Aucun commentaire:

Enregistrer un commentaire