参考自:https://msdn.microsoft.com/en-us/library/hh873132.aspx
1 #include <amp.h> 2 #include <iostream> 3 4 using namespace Concurrency; 5 6 // 缺省加速设备 7 void default_properties() 8 { 9 accelerator default_acc; 10 std::wcout << default_acc.device_path << "\n"; 11 std::wcout << default_acc.dedicated_memory << "\n"; 12 } 13 14 // 列出所有加速设备 15 void list_all_accelerators() 16 { 17 std::vector<accelerator> accs = accelerator::get_all(); 18 for (int i = 0; i < accs.size(); i++) 19 { 20 std::wcout << accs[i].device_path << "\n"; 21 std::wcout << accs[i].dedicated_memory << "\n"; 22 std::wcout << (accs[i].supports_cpu_shared_memory ? 23 "CPU shared memory: true" : "CPU shared memory: false") << "\n"; 24 std::wcout << (accs[i].supports_double_precision ? 25 "double precision:true" : "double precision: false") << "\n"; 26 std::wcout << (accs[i].supports_limited_double_precision ? 27 "limited double precision: true" : "limited double precision: false") << "\n"; 28 std::wcout << accs[i].description << "\n\n\n"; 29 } 30 } 31 32 // 选择最大加速设备 33 void pick_with_most_memory() 34 { 35 std::vector<accelerator> accs = accelerator::get_all(); 36 accelerator acc_chosen = accs[0]; 37 for (int i = 0; i < accs.size(); i++) 38 { 39 if (accs[i].dedicated_memory > acc_chosen.dedicated_memory) 40 { 41 acc_chosen = accs[i]; 42 } 43 } 44 45 std::wcout << "The accelerator with the most memory is " 46 << acc_chosen.device_path << "\n" 47 << acc_chosen.dedicated_memory << ".\n"; 48 } 49 50 // 和CPU共享内存 51 void sharedMemory() 52 { 53 accelerator acc = accelerator(accelerator::default_accelerator); 54 55 // Early out if the default accelerator doesn‘t support shared memory 56 if (!acc.supports_cpu_shared_memory) 57 { 58 std::cout << "The default accelerator does not support shared memory" << std::endl; 59 return; 60 } 61 62 // Override the default CPU access type 63 acc.set_default_cpu_access_type(access_type_read_write); 64 65 // Create an accelerator_view from the default acclerator. The 66 // accelerator_view reflects the default_cpu_access_type of the 67 // accelerator it‘s associated with. 68 accelerator_view acc_v = acc.default_view; 69 } 70 71 // 选择加速设备 72 bool pick_accelerator() 73 { 74 std::vector<accelerator> accs = accelerator::get_all(); 75 accelerator chosen_one; 76 77 auto result = std::find_if(accs.begin(), accs.end(), [](const accelerator& acc) 78 { 79 return !acc.is_emulated && acc.supports_double_precision && !acc.has_display; 80 }); 81 82 if (result != accs.end()) 83 { 84 chosen_one = *(result); 85 } 86 87 std::wcout << chosen_one.description << std::endl; 88 89 bool success = accelerator::set_default(chosen_one.device_path); 90 91 return success; 92 } 93 94 void main() 95 { 96 default_properties(); 97 //list_all_accelerators(); 98 //pick_with_most_memory(); 99 //sharedMemory(); 100 101 pick_accelerator(); 102 }
时间: 2024-09-26 16:39:03