Skip to content

Failed to allocate counts or TMP when assigning g in gforce #4295

Closed

Description

@renkun-ken

I'm working with a data.table of 1423657324 rows and 14 columns.

There's an integer group (grp) with 3790 unique values.

When I do the following

stats <- ft[, .(
      col1 = median(col1, na.rm = TRUE),
      col2 = median(col2, na.rm = TRUE),
      col3 = median(col3, na.rm = TRUE)
    ), keyby = grp]

The following error occurs:

Error in gforce(thisEnv, jsub, o__, f__, len__, irows) :
  Internal error: Failed to allocate counts or TMP when assigning g in gforce

This does not occur when a shorter version of the data (1/3 of the size) is done with the same aggregation.

Activity

  1. MichaelChirico commented on Mar 10, 2020

    @MichaelChirico
    Member

    I would imagine median could be very memory hungry, this is why you usually see approx_quantile in "big data" SQL and median itself is dropped entirely.

    Do you get the same issue if you try and sort the data by col1? If not, it may be that gmedian is trying to do all three columns at once.

    As a workaround, do ft[ , .N, keyby = .(grp, col1)] and get the median from the frequency table, since you said there are far fewer unique values.

  2. shrektan commented on Mar 10, 2020

    @shrektan
    Member

    Will changing the median to stats::median help? It should prevent data.table from optimizing with gforce.

  3. renkun-ken commented on Mar 10, 2020

    @renkun-ken
    MemberAuthor

    Disabling gforce or using stats::median will not trigger this error. But I'm still curious about why this occurs?

    My server memory is 1TB and should be enough to process.

  4. MichaelChirico commented on Mar 10, 2020

    @MichaelChirico
    Member

    how about stats::median(c(col1, col2, col3)) (just to check if tripling the memory footprint has an impact)

  5. renkun-ken commented on Mar 10, 2020

    @renkun-ken
    MemberAuthor

    stats::median(c(col1, col2, col3)) works quite normally and nothing special occurs.

  6. shrektan commented on Mar 10, 2020

    @shrektan
    Member

    Well, the error is thrown from

    data.table/src/gsumm.c

    Lines 114 to 115 in b1b1832

    int *counts = calloc(nBatch*highSize, sizeof(int)); // TODO: cache-line align and make highSize a multiple of 64
    int *TMP = malloc(nrow*2*sizeof(int));

    However, I also see the nrow is declared as an int

    static int nrow = 0; // length of underlying x; same as length(ghigh) and length(glow)

    It looks like it will lead to an overflow for 1423657324 * 2...

  7. shrektan commented on Mar 10, 2020

    @shrektan
    Member

    @renkun-ken Would you mind to have a test? Just cut the row count of your data to 1073741823L / 1073741824L respectively and try your original code.

    I'm expecting the first case (1073741823L row) works but the second will fail.

  8. shrektan commented on Mar 10, 2020

    @shrektan
    Member

    I believe that's the cause. The overflow leads to an infinite memory allocation...

    Rcpp::cppFunction("size_t test(int x) {
                        return x*2*sizeof(int);
                      }")
    test(1073741823L)
    #> [1] 8589934584
    test(1073741824L)
    #> [1] 1.844674e+19
    test(1423657324L)
    #> [1] 1.844674e+19

    Created on 2020-03-10 by the reprex package (v0.3.0)

  9. renkun-ken commented on Mar 10, 2020

    @renkun-ken
    MemberAuthor

    Yes, 1073741823L case works perfectly while 1073741824L case does not work as you expected.

  10. shrektan commented on Mar 12, 2020

    @shrektan
    Member

    @renkun-ken It would be great if you can verify that PR #4297 does fix this issue, when you have time, of course.

  11. renkun-ken commented on Mar 12, 2020

    @renkun-ken
    MemberAuthor

    @shrektan Thanks! I'll verify it soon.

  12. renkun-ken commented on Mar 12, 2020

    @renkun-ken
    MemberAuthor

    @shrektan sadly too hard to fetch the repo

    remote: Enumerating objects: 1431, done.
    remote: Counting objects: 100% (1431/1431), done.
    remote: Compressing objects: 100% (181/181), done.
    Timeout, server github.com not responding. KiB | 2.00 KiB/s  
    fatal: the remote end hung up unexpectedly
    fatal: early EOF
    fatal: index-pack failed
    

    tried many times but no luck

  13. shrektan commented on Mar 12, 2020

    @shrektan
    Member

    The network issue I encountered before... so I packaged the source and have sent it to your email (I assume you are able to access your personal email).

  14. renkun-ken commented on Mar 13, 2020

    @renkun-ken
    MemberAuthor

    Thanks for your nice packaging! My git fetch worked magically as I was sleeping last night.

    I retried and can confirm that the bug is fixed with the PR. Good work!

    BTW, are you suspecting that there are some other similar int problems like this?

  15. 1 remaining item

  16. renkun-ken commented on Mar 13, 2020

    @renkun-ken
    MemberAuthor

    Also BTW, for such bugs that require very large data to reproduce, do we need to build test cases for them?

    For this bug, a minimal reproducible example is

    library(data.table)
    
    n <- 1500000000
    ngrp <- 4000
    dt <- data.table(group = sample.int(ngrp, n, replace = TRUE), x = runif(n))
    res <- dt[, .(
      xmedian = median(x, na.rm = TRUE)
    ), keyby = group]

    but dt is 18GB and creating dt costs 5-10 mins.

  17. shrektan commented on Mar 13, 2020

    @shrektan
    Member

    You can get a faster example by getting rid of the random functions.... just use rep() and c()... I actually did try to test this.

    The difficulty is the requirement of a very large amount of memory...

  18. renkun-ken commented on Mar 13, 2020

    @renkun-ken
    MemberAuthor

    I tried

    dt <- data.table(group = rep(seq_len(ngrp), each = n / ngrp), x = numeric(n))

    and

    dt <- data.table(group = rep(seq_len(ngrp), each = n / ngrp), x = rep(rnorm(1000), each = n / 1000))

    Both are much faster but cannot produce the error. 👀

  19. ColeMiller1 commented on Mar 13, 2020

    @ColeMiller1
    Contributor

    Instead of rep(..., each =), remove the each. When the groupings are sorted, the part of code that uses TMP isn't used.

  20. steffen-windmueller commented on May 5, 2020

    @steffen-windmueller

    I got a similar problem. When running my R script in a Windows SQL Server (R version 3.5.2 and data.table 1.12.0), the following error occurs:
    Error in forderv(ans, cols, sort = TRUE, retGrp = FALSE, order = if (decreasing) -order else order, : Failed to allocate TMP or UGRP or they weren't cache line aligned: nth=8 Call: source ... [ -> [.data.table -> eval -> eval -> forder -> forderv
    that is called by the following line of forcer.c:
    src/forder.c:728: if (!TMP || !UGRP /*|| TMP%64 || UGRP%64*/) STOP(_("Failed to allocate TMP or UGRP or they weren't cache line aligned: nth=%d"), nth);

    The script joins two data tables (X and Y) with an on=.(Id,date>=start_date,date<=end_date) statement and uses by=.EACHI for the operation. When running the same script on my local RStudio version, there is no error. Do you think setting "Id" and "date" resp. "start_date" and "end_date" as keys in X and Y will prevent the integer overload? Alternatively, would changing by=.EACHI to keyby=.EACHI do the thing?

    Thank you already in advance.

  21. shrektan commented on May 5, 2020

    @shrektan
    Member

    Do you think setting "Id" and "date" resp. "start_date" and "end_date" as keys in X and Y will prevent the integer overload? Alternatively, would changing by=.EACHI to keyby=.EACHI do the thing?

    I don't have experience in MSSQLServer with R but I don't think it will solve your problem. Is it expensive to give a try?

  22. steffen-windmueller commented on May 5, 2020

    @steffen-windmueller

    I gave it a try and it did not work out. Do you know what might help here? Could it have to do something with the dependencies between data.table and bit64, since dependencies are sometimes distorted in MSSQL?

  23. shrektan commented on May 5, 2020

    @shrektan
    Member

    In my opinion, it should have nothing to do with bit64 because bit64 has stopped updating for 3 years. I have some (limited) suggestions for you:

    • Turn-off the multiple threads, i.e., data.table::setDTthreads(1L)
    • Use the dev version of data.table (albeit I doubt it will work)
    • Make a local reproducible example and report it here
    • Call for support from Microsoft if you have a business license
  24. steffen-windmueller commented on May 13, 2020

    @steffen-windmueller

    Turning off multiple threads did not work. Sometimes, the error changes to "invalid BXL stream", what can be attributed to having not enough memory.

    Would you know if the error src/forder.c:728: if (!TMP || !UGRP /*|| TMP%64 || UGRP%64*/) STOP(_("Failed to allocate TMP or UGRP or they weren't cache line aligned: nth=%d"), nth); can be caused by a lack of RAM?

  25. jangorecki commented on May 13, 2020

    @jangorecki
    Member

    @scharlatan0139 yes, it does look exactly like an error caused by lack of RAM.

  26. added
    GForce
    on Dec 4, 2020
  27. Debasis5 commented on Dec 9, 2020

    @Debasis5

    @shrektan , Is the rightway to import your fix is remotes::install_github("Rdatatable/data.table#fix4295")

    I tried this but got an invalid repo error

  28. shrektan commented on Dec 9, 2020

    @shrektan
    Member

    @Debasis5 It should be

    remotes::install_github("rdatatable/data.table#4297") 

    or

    remotes::install_github("Rdatatable/data.table@fix4295") 
  29. added this to the 1.13.5 milestone on Dec 9, 2020
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

    GForcebug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions