0Pricing
CUDA Academy · درس

استخدام cudaMemcpyAsync في Stream

عمليات نقل غير حاجبة يمكن أن تتداخل.

استخدام cudaMemcpyAsync في Stream درس مجاني في CUDA Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في CUDA Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة CUDA Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Blocking by Default

Plain cudaMemcpy stops your CPU until the copy finishes. Your host thread just waits, doing nothing useful while bytes move.

The Async Cousin

Meet cudaMemcpyAsync. It queues the copy and returns to your CPU immediately, so the host can keep working while the transfer happens.

It Needs a Stream

The async copy takes an extra argument: a stream. The stream is the ordered queue where the transfer waits its turn to run.

cudaMemcpyAsync(d_data, h_data, bytes, cudaMemcpyHostToDevice, stream);

Pinned or Bust

Here is the catch: async copies only run truly non-blocking from pinned host memory. Pass a pageable buffer and CUDA silently falls back to a blocking copy.

Returns, Does Not Finish

When the call returns, the copy has only been scheduled, not completed. The data may still be in flight, so do not read it yet.

Order Within a Stream

Work in one stream runs in order, one item after another. So a copy issued before a kernel in the same stream is guaranteed to finish first.

Wait When You Must

Before touching the results on the CPU, make sure the queue drained. Call cudaStreamSynchronize to block until that stream's work is done.

cudaStreamSynchronize(stream);

The Whole Point: Overlap

Async copies let a transfer in one stream run while a kernel in another stream computes. That concurrency is how you hide transfer time.

Beware the Default Stream

If you pass stream 0, the legacy default, it can serialize against everything else. Use your own created streams to actually get overlap.

Keep the Buffer Alive

Since the copy finishes later, the host buffer must stay valid until then. Free it too early and the in-flight transfer reads garbage.

A Typical Pattern

The classic flow is async copy in, launch the kernel, then async copy out, all in one stream. Sync only at the very end.

cudaMemcpyAsync(d_in, h_in, bytes, cudaMemcpyHostToDevice, stream);
kernel<<<grid, block, 0, stream>>>(d_in, d_out);

Quick Check

What does cudaMemcpyAsync require to be genuinely non-blocking?

Recap

cudaMemcpyAsync queues a copy in a stream and returns at once, but needs pinned memory to overlap. Sync the stream before reading results. ⚡

الأسئلة الشائعة

هل درس «استخدام cudaMemcpyAsync في Stream» مجاني؟

نعم — نص درس «استخدام cudaMemcpyAsync في Stream» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة CUDA Academy، انتقل إلى CoddyKit PRO. تتضمن دورة CUDA Academy 4 دروس في المجموع.

ماذا ستتعلم في «استخدام cudaMemcpyAsync في Stream»؟

عمليات نقل غير حاجبة يمكن أن تتداخل. تتمرن على CUDA Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ CUDA Academy؟

لا تُشترط خبرة سابقة. CUDA Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.

كم من الوقت يستغرق درس «استخدام cudaMemcpyAsync في Stream»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس CUDA Academy هذا؟

نعم. كل درس في CUDA Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. لماذا الذاكرة القابلة لترحيل الصفحات بطيئة
  2. الذاكرة المثبّتة باستخدام cudaMallocHost
  3. استخدام cudaMemcpyAsync في Stream
  4. خط أنابيب التخزين المزدوج
← العودة إلى CUDA Academy