<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <div class="moz-cite-prefix">Le 19/08/2021 à 17:41, Sokol Serguei a
      écrit :<br>
    </div>
    <blockquote type="cite"
      cite="mid:fdc9f673-abd3-774e-9259-877707129e56@gmail.com">
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <div class="moz-cite-prefix">Le 19/08/2021 à 17:04, Naeem
        Khoshnevis a écrit :<br>
      </div>
      <blockquote type="cite"
cite="mid:CAKOOZvMJjtXRUcn4WMKRajOo4a=4jE16NePrKmOT+G4g8St7Rg@mail.gmail.com">
        <meta http-equiv="content-type" content="text/html;
          charset=UTF-8">
        <div dir="ltr">
          <p
style="color:rgb(14,16,26);background:transparent;margin-top:0pt;margin-bottom:0pt"><span
style="background:transparent;margin-top:0pt;margin-bottom:0pt">Thank
              you so much, everyone, for responding to this email.</span></p>
          <p
style="color:rgb(14,16,26);background:transparent;margin-top:0pt;margin-bottom:0pt"><br>
          </p>
          <p
style="color:rgb(14,16,26);background:transparent;margin-top:0pt;margin-bottom:0pt"><span
style="background:transparent;margin-top:0pt;margin-bottom:0pt">Dirk,</span></p>
          <ul
style="color:rgb(14,16,26);background:transparent;margin-top:0pt;margin-bottom:0pt">
            <li
style="background:transparent;margin-top:0pt;margin-bottom:0pt;list-style-type:disc"><span
style="background:transparent;margin-top:0pt;margin-bottom:0pt">I didn't
                think about testing _equality_ of doubles because the
                numbers are significantly different (e.g., instead of
                0.5, chooses 1.5). However, that is a valid point, and I
                should be aware of that.</span></li>
            <li
style="background:transparent;margin-top:0pt;margin-bottom:0pt;list-style-type:disc"><span
style="background:transparent;margin-top:0pt;margin-bottom:0pt">You are
                right about the serial runs. Whenever I deactivate
                OpenMP, the results are correct.</span></li>
          </ul>
          <p
style="color:rgb(14,16,26);background:transparent;margin-top:0pt;margin-bottom:0pt"><br>
          </p>
          <p
style="color:rgb(14,16,26);background:transparent;margin-top:0pt;margin-bottom:0pt"><span
style="background:transparent;margin-top:0pt;margin-bottom:0pt">Serguei,</span></p>
          <ul
style="color:rgb(14,16,26);background:transparent;margin-top:0pt;margin-bottom:0pt">
            <li
style="background:transparent;margin-top:0pt;margin-bottom:0pt;list-style-type:disc"><span
style="background:transparent;margin-top:0pt;margin-bottom:0pt">Thanks
                for the comments. Yes, I agree. It seems outer is a
                better option. We have started with outer. However,
                outer builds the entire matrix of differences first,
                then finds the minimum index. In our application, it
                requires 200 GB of memory to build that matrix.</span></li>
          </ul>
        </div>
      </blockquote>
      <p>This problem was hardly foreseeable from examples in hand. But
        I still think that a pure R solution can be a runner:</p>
      <p>   ac=a*sc<br>
           bc=b*sc<br>
           out=vapply(seq_along(bc), function(i)
        which.min(abs(ac-bc[i])+cd[i]), integer(1))<br>
      </p>
    </blockquote>
    <p>Further optimizing: "+cd[i]" does not change the result of
      which.min() so it can be dropped. The same reasoning can be
      applied to "*sc".<br>
      As the problem is reduced to searching the index of the closest
      value in 'a' to 'b[i]', it can be solved in O(log(n)) time
      (instead of O(n)) by binary search if 'a' can be sorted before
      operations. If 'b' can also be sorted, then the whole timing can
      be further reduced. E.g. we can use findInterval() implementing
      such optimal searching. However, its result should be
      post-processed to find value indexes instead of interval ones and
      get back to original indexes instead of sorted ones. Something
      like:</p>
    <p>   oa=order(a)<br>
         ob=order(b)<br>
         ao=a[oa]<br>
         bo=b[ob]<br>
         i=findInterval(bo, ao) # main work is done here<br>
         ii=i+ifelse(i < n & ao[i+1]-bo < bo-ao[i], 1, 0)<br>
         out=oa[ii]<br>
         out[ob]=out<br>
    </p>
    <p>Serguei.<br>
    </p>
    <blockquote type="cite"
      cite="mid:fdc9f673-abd3-774e-9259-877707129e56@gmail.com">
      <p> </p>
      <p>Best,<br>
        Serguei.<br>
      </p>
      <blockquote type="cite"
cite="mid:CAKOOZvMJjtXRUcn4WMKRajOo4a=4jE16NePrKmOT+G4g8St7Rg@mail.gmail.com">
        <div dir="ltr">
          <ul
style="color:rgb(14,16,26);background:transparent;margin-top:0pt;margin-bottom:0pt">
            <li
style="background:transparent;margin-top:0pt;margin-bottom:0pt;list-style-type:disc"><span
style="background:transparent;margin-top:0pt;margin-bottom:0pt"> Rcpp
                does the job with around 10 MB. That is why I switched
                to Rcpp. Please let me know your thoughts.</span></li>
          </ul>
          <p
style="color:rgb(14,16,26);background:transparent;margin-top:0pt;margin-bottom:0pt"><br>
          </p>
          <h3
style="color:rgb(14,16,26);background:transparent;margin-top:0pt;margin-bottom:0pt"><span
style="background:transparent;margin-top:0pt;margin-bottom:0pt"><span
style="background:transparent;margin-top:0pt;margin-bottom:0pt;font-weight:normal">Iñaki,</span></span></h3>
          <ul
style="color:rgb(14,16,26);background:transparent;margin-top:0pt;margin-bottom:0pt">
            <li
style="background:transparent;margin-top:0pt;margin-bottom:0pt;list-style-type:disc"><span
style="background:transparent;margin-top:0pt;margin-bottom:0pt">Thanks
                for your suggestion. Yes, the problem is shared values,
                and it resolved the issue. I really appreciate it. </span></li>
          </ul>
          <div><font color="#0e101a"><br>
            </font></div>
          <div><font color="#0e101a">Best regards,</font></div>
          <div><font color="#0e101a">Naeem </font></div>
        </div>
        <br>
        <div class="gmail_quote">
          <div dir="ltr" class="gmail_attr">On Thu, Aug 19, 2021 at 4:56
            AM Iñaki Ucar <<a href="mailto:iucar@fedoraproject.org"
              moz-do-not-send="true">iucar@fedoraproject.org</a>>
            wrote:<br>
          </div>
          <blockquote class="gmail_quote" style="margin:0px 0px 0px
            0.8ex;border-left:1px solid
            rgb(204,204,204);padding-left:1ex">On Thu, 19 Aug 2021 at
            04:53, Dirk Eddelbuettel <<a href="mailto:edd@debian.org"
              target="_blank" moz-do-not-send="true">edd@debian.org</a>>
            wrote:<br>
            ><br>
            ><br>
            > Naeem,<br>
            ><br>
            > I would simplify, simplify, simplify -- as 'Rcpp FAQ
            7.31' reminds us all,<br>
            > testing _equality_ of doubles is challenging anyway.<br>
            ><br>
            > Besides, it may make sense to would ascertain first you
            get what you want in<br>
            > _purely serial modes_ and then move to OpenMP.<br>
            <br>
            Exactly. Serial execution should be fine. I.e., if you set
            the number<br>
            of threads to 1, then all platforms will return the same
            result.<br>
            However, you have defined a number of variables outside the
            parallel<br>
            region, and then you modify them inside the parallel region.
            OpenMP<br>
            takes those variables as shared by default, which leads to
            the<br>
            unexpected results you are getting. You need to tell OpenMP
            that those<br>
            variables are threadprivate. Or you could just define them
            inside the<br>
            parallel region, so that OpenMP knows that they are private
            without<br>
            additional hints.<br>
            <br>
            -- <br>
            Iñaki Úcar<br>
          </blockquote>
        </div>
        <br>
        <fieldset class="mimeAttachmentHeader"></fieldset>
        <pre class="moz-quote-pre" wrap="">_______________________________________________
Rcpp-devel mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Rcpp-devel@lists.r-forge.r-project.org" moz-do-not-send="true">Rcpp-devel@lists.r-forge.r-project.org</a>
<a class="moz-txt-link-freetext" href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" moz-do-not-send="true">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a></pre>
      </blockquote>
      <p><br>
      </p>
    </blockquote>
    <p><br>
    </p>
  </body>
</html>