مرحبًا بـ GPU: أول ملف .cu لك
أنشئ برنامج CUDA بسيطًا وشغّله.
مرحبًا بـ GPU: أول ملف .cu لك درس مجاني في CUDA Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في CUDA Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة CUDA Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Your First .cu File
Time to build something real. A CUDA program lives in a .cu file, which can hold both CPU and GPU code side by side. 🎉
Include the Header
Most CUDA samples start by pulling in standard headers. The CUDA runtime header is included automatically by nvcc, so iostream is often enough.
#include <cstdio>Marking a Kernel
A function that runs on the GPU is a kernel. You mark it with the __global__ qualifier so nvcc compiles it for the device.
__global__ void hello() {
}Printing from the GPU
Inside the kernel you can call printf just like on the CPU. Each GPU thread that runs it produces its own line of output.
__global__ void hello() {
printf("Hello from GPU\n");
}The Launch Syntax
To run a kernel you use the triple-angle-bracket launch. The numbers inside choose how many blocks and threads will execute it.
hello<<<1, 1>>>();Launches Are Asynchronous
A kernel launch returns immediately while the GPU works in the background. The CPU keeps going, which is powerful but can be surprising at first.
Waiting for the GPU
Because of that, you must synchronize before the program ends, or the printf output may never appear on screen.
cudaDeviceSynchronize();Putting It Together
Your main function launches the kernel and then waits. That is the entire skeleton of a working CUDA program.
int main() {
hello<<<1, 1>>>();
cudaDeviceSynchronize();
}Compile It
Save the file as hello.cu and build it with nvcc, naming the output program with the -o flag.
nvcc hello.cu -o helloRun It
Now run the program. Seeing Hello from GPU printed means your code truly executed on the device. That is your first GPU output! 🚀
./helloMany Threads at Once
Bump the launch to many threads and you will see the greeting printed several times, one line per thread. That is real parallelism in action.
hello<<<1, 8>>>();Quick Check
One detail makes or breaks this program.
Recap
You wrote a real CUDA program: a __global__ kernel, a triple-bracket launch, and a sync. Compile with nvcc, run it, and the GPU greets you. Congrats! 🎓
الأسئلة الشائعة
هل درس «مرحبًا بـ GPU: أول ملف .cu لك» مجاني؟
نعم — نص درس «مرحبًا بـ GPU: أول ملف .cu لك» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة CUDA Academy، انتقل إلى CoddyKit PRO. تتضمن دورة CUDA Academy 4 دروس في المجموع.
ماذا ستتعلم في «مرحبًا بـ GPU: أول ملف .cu لك»؟
أنشئ برنامج CUDA بسيطًا وشغّله. تتمرن على CUDA Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ CUDA Academy؟
لا تُشترط خبرة سابقة. CUDA Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «مرحبًا بـ GPU: أول ملف .cu لك»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس CUDA Academy هذا؟
نعم. كل درس في CUDA Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- إصدارات Driver وRuntime وToolkit
- قراءة nvidia-smi باحتراف
- الترجمة باستخدام nvcc
- مرحبًا بـ GPU: أول ملف .cu لك