FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Program is running out of GPU memory space / Problem with while loop · Issue #2795 · arrayfire/arrayfire · GitHub

Repository navigation

Program is running out of GPU memory space / Problem with while loop #2795

Description

Dear arrayfire programmers,

I'm trying to use arrayfire for an signal processing application (written in C) with large amounts of data. The occurring problem is that the arrayfire arrays, which are used inside an (indefinitely long running) while loop, use up more and more of the GPU memory until it runs out of space. Each iteration of the loop adds five new memory pointers of about 1 MB size.

This example shows my problem (of course it's watered down to the essential problem, the real program features some external structures, however the problem also occurs with this example):

`int cycle = 0;
int MAX = 100000;
int signal_length = 32;
int window_length = 8;

float signal[signal_length];
float window[window_length];

for (int i = 0; i<signal_length; i++) {
signal[i] = cos(2M_PI/(fft_length(i+0.5)*2));
}

for (int i = 0; i<window_length; i++) {
window[i] = sin(M_PIi1./(fft_length*-1));
}

af_array d_signal, d_window;
int n_dims = 4;
dim_t signal_dims[] = {fft_length, signal_length/fft_length,1,1};
dim_t window_dims[] = {fft_length,1,1,1};

af_constant(&d_signal,0.,n_dims,signal_dims,f32);
af_constant(&d_window,0.,n_dims,window_dims,f32);

while(cycle < MAX) {
af_write_array(d_signal,(void*)signal,sizeof(signal),f32);
af_write_array(d_window,(void*)window,sizeof(window),f32);
af_mul(&d_signal, d_signal, d_window, 1);
af_fft(&d_signal,d_signal,1,fft_length);
af_print_array(d_signal);

const char* msf;
af_print_mem_info(&msg,0);
printf("%s\n",msg);
af_device_gc();
getchar();
}`

So the result of this is an increasing number of used up space and of stored arrays. Is there any chance to get rid of arrays, which are not needed anymore? Since the memory manager saves most of the arrays as AF LOCK 'Yes', I am not able to get rid of them.

Any help would be appreciated.
Cheers!

Activity

  1. cschreib-ibex commented on Mar 16, 2020

    Contributor

    My guess is you forgot to call af_release_array() to tell AF when arrays are not used anymore. For example when you call af_mul(&d_signal, d_signal, d_window, 1) you overwrite the handle of d_signal, and loose the original handle. This should be replaced by

    af_array temp;
    af_mul(&temp, d_signal, d_window, 1);
    af_release_array(d_signal);
    d_signal = temp;
  2. Fabsch93 commented on Mar 16, 2020

    Author

    Well, that looks promising! The first tries seemed to have solved the problem. Thank you very much!
    Hopefully this call will be documented better in the future, as I have never seen it before and the documentation only has a short, uninformative information about af_relase_array()

  3. 9prady9 commented on Mar 16, 2020

    Member

    @Fabsch93 Sorry about the inconvenience. What @cschreib-ibex suggested could very well be the reason behind what you are experiencing. To add to what he suggested, all API calls that has the suffix af_create should have a matching API call with af_release suffix with appropriate input parameters. Apart from that, all functions allocate (using ArrayFire memory manager - pooled fashion) memory required for populating the output. Therefore, you would have to call af_release_array on all output af_arrays of such function calls. All of this is hidden and taken care of automatically by af::array class in C++ API. If you have to use C-API, then you have to take care of all af_release calls on your own.

    In the documentation of af_release_array, we do say that a call to it will "Reduce the reference count of the af_array". Reference counting is a common technique to handle resource management in programming. Hence, we may have assumed it would be understood. Perhaps, we can explicitly hyperlink af_release_array call in the documentation of af_create_array. That should help I think. I have raised the PR with said change - #2797 .

    Having said that, please note that we encourage our users to use C++ API to help with resource management, terse syntax etc. unless there is a strong and unavoidable reason for not using the C++ API. Are you using a C(only) compiler to compile your application ?

  4. Fabsch93 commented on Mar 16, 2020

    Author

    @9prady9 Thank you for your answer, the support here ist really great. I will soon be testing this way of releasing arrays in my full program, however, in the standalone code it worked well.

    Also thank you for explaining why it is not mentioned in the documentation. I first started using the C++ API in a standalone program (just for testing) and no problems occurred there. Since the real program is a quite extensive one with interaction between server and client, which was build by a Ph.D. student before me in C, I was not intrigued by changing up everything to use the C++ API. I tried it, but failed miserably and so I went back to C API. On a positive note, I will need only basic Arrayfire usage (FFT, Multiplication, Mean), so I'm pretty sure that C API will work for me.

  5. 9prady9 commented on Mar 16, 2020

    Member

    In that case, do make sure that all af_arrays are released appropriately. If the number functions you use is smaller, it shouldn't be hard to manage the release calls. In any case, you can also reach out to us on our slack channel https://join.slack.com/t/arrayfire-org/shared_invite/MjI4MjIzMDMzMTczLTE1MDI5ODg4NzYtN2QwNGE3ODA5OQ for any questions you may have in the future.

  6. 9prady9 commented on Mar 23, 2020

    Member

    @Fabsch93 We have reorganized C API a little for better grouping. #2797 You will see this reflected in next release docs update.

  7. Fabsch93 commented on Mar 26, 2020

    Author

    @9prady9 Sorry to bother you again, but is there any chance to select specific data from an array within the C API? The only workaround I can think of right now is copying the array back to the host, selecting the wanted data points and copy it back to the device. To my understanding, things like col or row only work in C++, as I cannot find anything like af_col or similar.

    The thing I need would enable me to copy e.g. the first two rows of an exisiting array to a new array or similarly delete all rows except the first two.

  8. 9prady9 commented on Mar 26, 2020

    Member

    @Fabsch93 You would have to use C API from the following group of functions.

    Assignment & Indexing operation on arrays

    col, row, slice are all written on top of these functions in C++ API.

    You can review assign.cpp and index.cpp test code in our github repository to understand how to use C API for indexing/assignment.

  9. Fabsch93 commented on Mar 26, 2020

    Author

    @9prady9 Thank you so much! I guess now my program is finally able to shoot for the moon.

  10. 9prady9 commented on Mar 26, 2020

    Member

    @Fabsch93 👍 Good luck, glad to be of help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL