• Visual Studio์—์„œ CUDA ์‹คํ–‰ํ•˜๊ธฐ

    2024. 12. 18.

    by. ํ•ด๋Š”์„ 

    cuda๋ฅผ visual studio ํ™˜๊ฒฝ์—์„œ ์‹คํ–‰ํ•  ์ผ์ด ์ƒ๊ฒจ์„œ ์ •๋ฆฌ๊ฒธ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.

    ๋น ๋ฅธ ์ฝ”๋“œ ์ ์šฉ์„ ์œ„ํ•ด nvidia์—์„œ ์ œ๊ณตํ•˜๋Š” ๋‹ค์Œ ์˜ˆ์ œ๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

     

    https://github.com/NVIDIA/cuda-samples?tab=readme-ov-file

     

    GitHub - NVIDIA/cuda-samples: Samples for CUDA Developers which demonstrates features in CUDA Toolkit

    Samples for CUDA Developers which demonstrates features in CUDA Toolkit - NVIDIA/cuda-samples

    github.com

     

    ์ €๋Š” Windows 10 ํ™˜๊ฒฝ์— NVIDIA TITAN RTX, Visual Studio 2022 ํ™˜๊ฒฝ์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

     

     

    1. Visual studio 2022์— C++ ํ™˜๊ฒฝ ์„ค์น˜

    C++์„ ์‚ฌ์šฉํ•œ ๋ฐ์Šคํฌํ†ฑ ๊ฐœ๋ฐœ์ด ์„ค์น˜๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

     

     

    2. CUDA/CuDNN ์„ค์น˜

    ํ˜„์žฌ cuda-sample์—์„œ๋Š” CUDA 12.5๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ €๋„ ์—ฌ๊ธฐ์— ๋งž์ถฐ CUDA 12.5๋ฅผ ์„ค์น˜ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

    CUDA/CuDNN ์„ค์น˜ํ•˜๋Š” ๋ฒ•์€ ์ธํ„ฐ๋„ท์— ๋งŽ์ด ๋‚˜์˜ค๋‹ˆ๊นŒ ์ƒ๋žต...

    https://lonaru-burnout.tistory.com/16

     

    [ML][Windows 11] CUDA, cuDNN ์„ค์น˜

    ์ด ๊ธ€์€ NVIDIA ๊ทธ๋ž˜ํ”ฝ์นด๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ž‘์„ฑ๋˜์—ˆ์Œ์„ ์•Œ๋ฆฝ๋‹ˆ๋‹ค. ๋ผ๋ฐ์˜จ ๊ทธ๋ž˜ํ”ฝ์นด๋“œ๋ฅผ ํ†ตํ•œ ๋จธ์‹ ๋Ÿฌ๋‹์€ ๋”ฐ๋กœ ๋‹ค๋ฃฐ ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. 0. ์ž์‹ ์˜ GPU ํ™•์ธ https://ko.wikipedia.org/wiki/CUDA CUDA - ์œ„ํ‚ค๋ฐฑ๊ณผ, ์šฐ๋ฆฌ ๋ชจ

    lonaru-burnout.tistory.com

     

    ์„ค์น˜ ์™„๋ฃŒ๋œ ๋ชจ์Šต

     

     

    3. Visual studio project ์ƒ์„ฑ

    Visual studio๋ฅผ ์‹คํ–‰ํ•˜๊ณ  "์ƒˆ ํ”„๋กœ์ ํŠธ ๋งŒ๋“ค๊ธฐ" ํด๋ฆญ

     

    ๋ชจ๋“  ์–ธ์–ด์—์„œ CUDA ๋ฅผ ์„ ํƒํ•˜๋ฉด ์„ค์น˜ํ•œ ๋ฒ„์ „์— ๋”ฐ๋ฅธ ํ…œํ”Œ๋ฆฟ์ด ๋‚˜์˜ต๋‹ˆ๋‹ค. ์ €๋Š” 11.8๋ฒ„์ „๋„ ๊ฐ™์ด ๊น”๋ ค์žˆ์–ด์„œ ๋‘˜๋‹ค ๋‚˜์˜ค๋„ค์š”. ์ผ๋‹จ 12.5๋ฅผ ์„ ํƒํ•˜์—ฌ ์ƒˆ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

     

    ๊ทธ๋Ÿผ ๊ธฐ๋ณธ ํ™”๋ฉด์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

     

     

     

    4. ์›ํ•˜๋Š” sample์˜ main.cpp๋ฅผ ์˜ฎ๊ฒจ์ฃผ๊ธฐ

    kernel.cu๋ฅผ ์‚ญ์ œํ•˜๊ณ , ์†”๋ฃจ์…˜ ํƒ์ƒ‰๊ธฐ์—์„œ ์™ผ์ชฝํด๋ฆญ -> ์ถ”๊ฐ€ -> ์ƒˆ ํ•ญ๋ชฉ ์œผ๋กœ main.cpp๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ , ์›ํ•˜๋Š” ์ƒ˜ํ”Œ์˜ main.cpp๋ฅผ ๋ณต์‚ฌ-๋ถ™์—ฌ๋„ฃ๊ธฐ ํ•ฉ๋‹ˆ๋‹ค. ์ €๋Š” conjugateGradient ์˜ˆ์ œ๋ฅผ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค.

     

     

    ์ด๋ฅผ ์‹คํ–‰ ํ˜น์€ ๋นŒ๋“œํ•˜๋ฉด ์—๋Ÿฌ๊ฐ€ ๋‚ฉ๋‹ˆ๋‹ค.

     

    cuda-sample์„ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ์ถ”๊ฐ€ ์ข…์†์„ฑ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด์ œ ์ด๋ฅผ ์—ฐ๊ฒฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

     

     

     

    5. Common ๋„ฃ์–ด์ฃผ๊ธฐ (ํ—ค๋”ํŒŒ์ผ) ๋ฐ ํ—ค๋” ์—ฐ๊ฒฐ

    ์™„์ „ ๋…ธ๋ฒ ์ด์Šค๋กœ ์ฟ ๋‹ค ์ฝ”๋”ฉ์„ ์‹œ์ž‘ํ•˜๋Š”๊ฑด ๋„ˆ๋ฌด ์–ด๋ ต๊ธฐ ๋•Œ๋ฌธ์—, sample์„ ์ ์ ˆํžˆ ํ™œ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด์„œ cuda-sample ํ™˜๊ฒฝ๊ณผ ๋˜‘๊ฐ™์ด ๋งŒ๋“ค์–ด ์ค„ ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋“ค์˜ ํ™˜๊ฒฝ๊ณผ ๋งž์ถฐ์ฃผ๋ ค๋ฉด, ์™ธ๋ถ€ ์ข…์†์„ฑ์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

     

    ๋จผ์ €, Cuda-sample ์˜ "common" ํด๋”๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“  ํ”„๋กœ์ ํŠธ ํ™˜๊ฒฝ ํด๋”๋กœ ์˜ฎ๊ฒจ์ค๋‹ˆ๋‹ค. 

    ์—ฌ๊ธฐ์—๋Š” ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ํ—ค๋”๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค.

    ํ”„๋กœ์ ์…˜์˜ ์™ธ๋ถ€ ์ข…์†์„ฑ์— ๋„ฃ์–ด์ฃผ๋ ค๋ฉด ์ถ”๊ฐ€ ํฌํ•จ ๋””๋ ‰ํ„ฐ๋ฆฌ์— ๋„ฃ์–ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.

     

    ๋จผ์ €, sample์˜ solution ํŒŒ์ผ์„ ์—ด๊ณ , ํ”„๋กœ์ ํŠธ -> ์†์„ฑ์„ ์—ด์–ด์ค๋‹ˆ๋‹ค.

    ๊ทธ๋ฆฌ๊ณ  C/C++ -> ์ผ๋ฐ˜์„ ์—ด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ "์ถ”๊ฐ€ ํฌํ•จ ๋””๋ ‰ํ„ฐ๋ฆฌ" ๊ฐ’์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

    ์ด ๊ฐ’์„ ์ƒˆ๋กœ ๋งŒ๋“  ํ”„๋กœ์ ํŠธ์˜ ๊ฐ™์€ ์œ„์น˜์— ๋ณต์‚ฌ-๋ถ™์—ฌ๋„ฃ๊ธฐ ํ•ฉ๋‹ˆ๋‹ค.

    ๊ทธ๋Ÿฐ๋ฐ ์ด๋•Œ!!! Common์˜ ๊ฒฝ๋กœ์—์„œ ์•ž์— ์žˆ๋Š” "../../../"์„ "./"๋กœ ๋ณ€๊ฒฝํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

    sample ์˜ˆ์ œ๋“ค์—์„œ๋Š” common์„ ์˜ˆ์ œ ์†”๋ฃจ์…˜์˜ ์ƒ์œ„/์ƒ์œ„ ํด๋”์— ๋‘ฌ์„œ ์ €๋ ‡๊ฒŒ ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ–ˆ๋Š”๋ฐ, ์šฐ๋ฆฌ๋Š” ์†”๋ฃจ์…˜๊ณผ ๊ฐ™์€ ์œ„์น˜์— ๋‘๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

     

     

    ๊ทธ๋ž˜์„œ ๊ธฐ์กด์— ์ด๋Ÿฐ ์ƒํƒœ์—์„œ

     

     

    ์ด๋ ‡๊ฒŒ ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

     

    ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ CUDA C/C++ -> Common์—์„œ Additional Include Directories์˜ ๊ฒฝ๋กœ๋„ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

     

    ์ƒ˜ํ”Œ ์†”๋ฃจ์…˜์˜ ์ด ๊ฒฝ๋กœ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ

     

    ์šฐ๋ฆฌ ํ”„๋กœ์ ํŠธ๋„ ์ด๋ ‡๊ฒŒ ๋ฐ”๊ฟ”์ค๋‹ˆ๋‹ค.

    ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด header๊ฐ€ ํฌํ•จ๋˜๋ฉด์„œ ์™ธ๋ถ€ ์ข…์†์„ฑ์— Common์˜ ํ—ค๋”๋“ค์ด ๋ชจ๋‘ ๋“ค์–ด๊ฐ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

     

    ๊ทผ๋ฐ ์ด๋ ‡๊ฒŒ ํ•œ ๋’ค ์‹คํ–‰ํ•ด๋„ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

     

    LNK2019 ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š”๋ฐ, ์ด๋Š” header๊ฐ€ ํฌํ•จ๋œ ๊ฒƒ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊นŒ์ง€ ๋งํฌํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์•„๋ž˜ stackoverflow๋ฅผ ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.

    https://stackoverflow.com/questions/13570285/how-to-link-library-e-g-cublas-cusparse-for-cuda-on-windows

     

    how to link library (e.g. CUBLAS, CUSPARSE) for CUDA on windows

    I am trying to compile a cuda program which uses cusparse library. I am getting linking error: kernel.cu.obj : error LNK2019: unresolved external symbol _cusparseSetMatIndexBase@8 referenced in

    stackoverflow.com

     

    ์จ‹๋“  ๊ทธ๋ž˜์„œ ์ด๊ฑด ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ•˜๋‚˜ ๋ณด๋ฉด, ์•„๊นŒ ์—ด์—ฌ๋‘” ์†์„ฑ ํŽ˜์ด์ง€์—์„œ ๋ง์ปค -> ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค.

    ๊ทธ๋Ÿฌ๋ฉด ์ถ”๊ฐ€ ์ข…์†์„ฑ์— ์žˆ๋Š” ๊ฐ’๋“ค์ด ๋ณด์ด๋Š”๋ฐ, ์ด๋ฅผ ๊ทธ๋Œ€๋กœ ๋ณต์‚ฌ -> ๋ถ™์—ฌ๋„ฃ๊ธฐ ํ•ด์ค๋‹ˆ๋‹ค.

    ์ƒ˜ํ”Œ์˜ ์ด ๊ฐ’์„

     

    ์š”๋ ‡๊ฒŒ ๋ถ™์—ฌ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

     

    ๊ทธ๋Ÿฌ๊ณ  ์ ์šฉํ•˜๊ณ  ์‹คํ–‰ํ•˜๋ฉด

     

    ์งœ์ž”~ ์ด๋ ‡๊ฒŒ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

    ๋Œ“๊ธ€