Migrating to SYCL
One-stop forum for getting assistance migrating your existing code to SYCL
43 Discussions

dpct::device_vector::resize(size_type new_size, const T &x = T()) not working properly when x != T()

fxzjshm
Beginner
2,155 Views

Hi there.

The function `resize(size_type new_size, const T &x = T())` in `dpct::device_vector` of DPC++ Compatibility Tool doesn't really fill newly allocated memory with `x` , but I think this behaviour is indicated by the function signature.

The code

void resize(size_type new_size, const T &x = T()) {
  reserve(new_size);
  _size = new_size;
}

(at `/opt/intel/oneapi/dpcpp-ct/2022.0.0/include/dpct/dpl_extras/vector.h`, line 227-229, version 2022.0.0)

seems not involving `x`.

 

For reference,  the description at cppreference.com   says

If the current size is less than count,

2) additional copies of value are appended.

 

And the code in NVIDIA's Thrust

template<typename T, typename Alloc>
void vector_base<T,Alloc>
 ::resize(size_type new_size, const value_type &x)
{
  if(new_size < size())
  {
    iterator new_end = begin();
    thrust::advance(new_end, new_size);
    erase(new_end, end());
  } // end if
  else
  {
    insert(end(), new_size - size(), x);
  } // end else
} // end vector_base::resize()

(at `/usr/local/cuda/include/thrust/detail/vector_base.inl`, line 329-343, CUDA version 11.3)

does what it says.

 

I encountered this since last summer when migrating some CUDA program to oneAPI using DPCT (I mean dpct translates `thrust::device_vector::resize()` to `dpct::device_vector::resize()` which leads to different behaviour of two versions of the program), but it seems that it hasn't changed since then.

I do not mean to offend, but may I ask, is it a bug or a feature?

0 Kudos
7 Replies
ManjulaC_Intel
Moderator
2,115 Views

Hi,

Thanks for reaching out to us.

We are working on your issue internally and will get back to you soon.


Thanks & Regards,

Manjula



0 Kudos
ManjulaC_Intel
Moderator
2,071 Views

Hi,


We are also able to reproduce the same issue on our end.


>>I mean dpct translates `thrust::device_vector::resize()` to `dpct::device_vector::resize()` which leads to different behaviour of two versions of the program.

Can you expand more on this?


Thanks & Regards,

Manjula


0 Kudos
fxzjshm
Beginner
2,045 Views

Hi Manjula, thanks for your reply, and sorry for the late response.

 

The sentence you quoted is just a senario that may trigger this inconsistence.

Consider this sample code:

 

 

#include <thrust/device_vector.h>
#include <iostream>

int main() {
    thrust::device_vector<int> d_tmp;
    d_tmp.resize(5, 233);
    for(auto x : d_tmp) {
        std::cout << x << " ";
    }
    std::cout << std::endl;
    return 0;
}

 

 

Expected output is "233 233 233 233 233 ".

dpct translates this code into

 

 

#include <oneapi/dpl/execution>
#include <oneapi/dpl/algorithm>
#include <CL/sycl.hpp>
#include <dpct/dpct.hpp>
#include <dpct/dpl_utils.hpp>
#include <iostream>

int main() {
    dpct::device_vector<int> d_tmp;
    d_tmp.resize(5, 233);
    for(auto x : d_tmp) {
        /*
        DPCT1007:0: Migration of this CUDA API is not supported by the Intel(R)
        DPC++ Compatibility Tool.
        */
        std::cout << x << " ";
    }
    std::cout << std::endl;
    return 0;
}

 

 

which gives the result "0 0 0 0 0 ".

 

 

A more detailed senario (pseudo code):

 

 

thrust::device_vector<float> d_in;
thrust::device_vector<int> d_out;
thrust::device_vector<size_t> d_param_inds;
std::vector<float> parameters;

// user inputs parameters here

for(size_t i = 0; i < parameters.size(); i++) {
    some_search_algorithm(d_in, d_out, parameters[i]);
    d_param_inds.resize(d_out.size(), i);
}

 

 

I have some different values of a parameter of some search algorithm inputed by user, these parameters values are stored in a std::vector .

I use d_out to store results, and d_param_inds to store the index of the parameter this search uses. (In practice the parameter may be a huge struct and expensive to copy, so just store the index here.)

In this case, the `resize()` is important, but the dpct::vector cannot work as expected, it just stores 0.

 

Thank you for reading this far.


0 Kudos
ManjulaC_Intel
Moderator
2,028 Views

Hi,

 

We are able to reproduce the same issue on our end. I am checking your issue with the concerned team, and waiting for the feedback. I will get back to you with an update.

 

Thanks for reporting this issue.

 

Thanks & Regards,

Manjula 

 

0 Kudos
ManjulaC_Intel
Moderator
1,859 Views

Hi,


Thank you for your patience. The issue raised by you has been fixed in oneAPI 2022.2(in Compatibility tool 2022.1)​version. Please download and let us know if this resolves your issue.


Regards,

Manjula


0 Kudos
ManjulaC_Intel
Moderator
1,847 Views

Hi,

 

I have not heard back from you. This thread will no longer be monitored by Intel.

If you need further assistance, please post a new question.

 

Thanks & Regards,

Manjula



0 Kudos
fxzjshm
Beginner
1,836 Views

Sorry for the late response, confirm fixed.

Thank you!

0 Kudos
Reply