MESSAGE
DATE | 2016-10-31 |
FROM | Christopher League
|
SUBJECT | Re: [Learn] cudaMallac
|
From learn-bounces-at-nylxs.com Mon Oct 31 17:24:39 2016 Return-Path: X-Original-To: archive-at-mrbrklyn.com Delivered-To: archive-at-mrbrklyn.com Received: from www.mrbrklyn.com (www.mrbrklyn.com [96.57.23.82]) by mrbrklyn.com (Postfix) with ESMTP id 94756161312; Mon, 31 Oct 2016 17:24:39 -0400 (EDT) X-Original-To: learn-at-nylxs.com Delivered-To: learn-at-nylxs.com Received: from liucs.net (contrapunctus.net [174.136.110.10]) by mrbrklyn.com (Postfix) with ESMTP id 415CA161311 for ; Mon, 31 Oct 2016 17:24:36 -0400 (EDT) Received: from localhost (unknown [148.4.40.11]) by liucs.net (Postfix) with ESMTPSA id C284FE08E; Mon, 31 Oct 2016 17:24:34 -0400 (EDT) From: Christopher League To: Ruben Safir , Samir Iabbassen , learn-at-nylxs.com In-Reply-To: References: User-Agent: Notmuch/0.21 (http://notmuchmail.org) Emacs/25.1.1 (x86_64-unknown-linux-gnu) Date: Mon, 31 Oct 2016 17:24:34 -0400 Message-ID: <87r36w8b59.fsf-at-contrapunctus.net> MIME-Version: 1.0 Subject: Re: [Learn] cudaMallac X-BeenThere: learn-at-nylxs.com X-Mailman-Version: 2.1.17 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: multipart/mixed; boundary="===============1024648616==" Errors-To: learn-bounces-at-nylxs.com Sender: "Learn"
--===============1024648616== Content-Type: multipart/alternative; boundary="=-=-="
--=-=-= Content-Type: text/plain
Ruben Safir writes:
> Why are we using the ADDRESS of a pointer to a float as in &d_in
Because the first parameter of `cudaMalloc` is what, in some programming languages, would be called an "output parameter". The `cudaMalloc` function is going to overwrite the value of the pointer with a new pointer to the allocated memory. For it to do that, we have to pass the address of the pointer.
In C++, we could do it as a "reference parameter" instead, that type would be `void*&` and then you don't have to do `&d_in` but just `d_in`. (It still passes the address behind the scenes.)
Compare `cudoMalloc` to the regular `malloc` in `stdlib.h` -- it *returns* the pointer, so there's no need for an "output parameter".
~~~~ {.cpp} float* d_in = (float*) malloc(ARRAY_BYTES); ~~~~
CL
~~~~ {.cpp} #include
int main(int argc, char ** argv) { const int ARRAY_SIZE = 64; const int ARRAY_BYTES = sizeof(float) * ARRAY_SIZE; //generate the input array on host float h_in[ARRAY_SIZE]; float h_out[ARRAY_SIZE]; for( int i = 0; i < ARRAY_SIZE; i++) { h_in[i] = float(i); } //declare gpu memory pointers float *d_in *d_out; cudaMalloc(( void **) &d_in, ARRAY_BYTES ); cudaMalloc(( void **) &d_out, ARRAY_BYTES ); } ~~~~
--=-=-= Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
1.0, user-scalable=3Dyes">
Ruben Safir ruben-at-mrbrklyn.com= writes:
Why are we using the ADDRESS of a pointer to a float as in &d_in
Because the first parameter of cudaMalloc is what, in some = programming languages, would be called an =E2=80=9Coutput parameter=E2=80= =9D. The cudaMalloc function is going to overwrite the value o= f the pointer with a new pointer to the allocated memory. For it to do that= , we have to pass the address of the pointer.
In C++, we could do it as a =E2=80=9Creference parameter=E2=80=9D instea= d, that type would be void*& and then you don=E2=80=99t ha= ve to do &d_in but just d_in . (It still passe= s the address behind the scenes.)
Compare cudoMalloc to the regular malloc in ode>stdlib.h =E2=80=93 it returns the pointer, so there=E2= =80=99s no need for an =E2=80=9Coutput parameter=E2=80=9D.
ceCode cpp"> float* d_in =3D (>float*) malloc(ARRAY_BYTES);
CL
ceCode cpp">#include <stdio.h>
int main(int argc, n class=3D"dt">char ** argv) { const int ARRAY_SIZE = =3D 64; const int ARRAY_BYTES= =3D sizeof(float) * AR= RAY_SIZE; //generate the input array on host float h_in[ARRAY_SIZE]; float h_out[ARRAY_SIZE]; for( int i =3D class=3D"dv">0; i < ARRAY_SIZE; i++) { h_in[i] =3D float(i); } //declare gpu memory pointers float *d_in *d_out; cudaMalloc(( void **) &d_in, ARRAY_BYTES ); cudaMalloc(( void **) &d_out, ARRAY_BYTES ); }
--=-=-=--
--===============1024648616== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline
_______________________________________________ Learn mailing list Learn-at-nylxs.com http://lists.mrbrklyn.com/mailman/listinfo/learn
--===============1024648616==--
|
|