12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089 |
- /* StarPU --- Runtime system for heterogeneous multicore architectures.
- *
- * Copyright (C) 2009-2017 Université de Bordeaux
- * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 CNRS
- * Copyright (C) 2017 Inria
- * Copyright (C) 2013 Corentin Salingue
- *
- * StarPU is free software; you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published by
- * the Free Software Foundation; either version 2.1 of the License, or (at
- * your option) any later version.
- *
- * StarPU is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- * See the GNU Lesser General Public License in COPYING.LGPL for more details.
- */
- #ifdef STARPU_USE_CUDA
- #ifndef _GNU_SOURCE
- #define _GNU_SOURCE 1
- #endif
- #include <sched.h>
- #endif
- #include <stdlib.h>
- #include <math.h>
- #include <starpu.h>
- #include <starpu_cuda.h>
- #include <starpu_opencl.h>
- #include <common/config.h>
- #ifdef HAVE_UNISTD_H
- #include <unistd.h>
- #endif
- #include <core/workers.h>
- #include <core/perfmodel/perfmodel.h>
- #include <core/simgrid.h>
- #include <core/topology.h>
- #include <common/utils.h>
- #include <drivers/mpi/driver_mpi_common.h>
- #ifdef STARPU_USE_OPENCL
- #include <starpu_opencl.h>
- #endif
- #ifdef STARPU_HAVE_WINDOWS
- #include <windows.h>
- #endif
- #if defined(HAVE_DECL_HWLOC_CUDA_GET_DEVICE_OSDEV_BY_INDEX) && HAVE_DECL_HWLOC_CUDA_GET_DEVICE_OSDEV_BY_INDEX
- #include <hwloc/cuda.h>
- #endif
- #define SIZE (32*1024*1024*sizeof(char))
- #define NITER 32
- #ifndef STARPU_SIMGRID
- static void _starpu_bus_force_sampling(void);
- #endif
- /* timing is in µs per byte (i.e. slowness, inverse of bandwidth) */
- struct dev_timing
- {
- int numa_id;
- double timing_htod;
- double latency_htod;
- double timing_dtoh;
- double latency_dtoh;
- };
- /* TODO: measure latency */
- static double bandwidth_matrix[STARPU_MAXNODES][STARPU_MAXNODES];
- static double latency_matrix[STARPU_MAXNODES][STARPU_MAXNODES];
- static unsigned was_benchmarked = 0;
- #ifndef STARPU_SIMGRID
- static unsigned ncpus = 0;
- #endif
- static unsigned nnumas = 0;
- static unsigned ncuda = 0;
- static unsigned nopencl = 0;
- static unsigned nmic = 0;
- static unsigned nmpi_ms = 0;
- /* Benchmarking the performance of the bus */
- static double numa_latency[STARPU_MAXNUMANODES][STARPU_MAXNUMANODES];
- static double numa_timing[STARPU_MAXNUMANODES][STARPU_MAXNUMANODES];
- #ifndef STARPU_SIMGRID
- static uint64_t cuda_size[STARPU_MAXCUDADEVS];
- #endif
- #ifdef STARPU_USE_CUDA
- /* preference order of cores (logical indexes) */
- static int cuda_affinity_matrix[STARPU_MAXCUDADEVS][STARPU_MAXNUMANODES];
- #ifndef STARPU_SIMGRID
- #ifdef HAVE_CUDA_MEMCPY_PEER
- static double cudadev_timing_dtod[STARPU_MAXNODES][STARPU_MAXNODES] = {{0.0}};
- static double cudadev_latency_dtod[STARPU_MAXNODES][STARPU_MAXNODES] = {{0.0}};
- #endif
- #endif
- static struct dev_timing cudadev_timing_per_numa[STARPU_MAXCUDADEVS*STARPU_MAXNUMANODES];
- static char cudadev_direct[STARPU_MAXNODES][STARPU_MAXNODES];
- #endif
- #ifndef STARPU_SIMGRID
- static uint64_t opencl_size[STARPU_MAXCUDADEVS];
- #endif
- #ifdef STARPU_USE_OPENCL
- /* preference order of cores (logical indexes) */
- static int opencl_affinity_matrix[STARPU_MAXOPENCLDEVS][STARPU_MAXNUMANODES];
- static struct dev_timing opencldev_timing_per_numa[STARPU_MAXOPENCLDEVS*STARPU_MAXNUMANODES];
- #endif
- #ifdef STARPU_USE_MIC
- static double mic_time_host_to_device[STARPU_MAXNODES] = {0.0};
- static double mic_time_device_to_host[STARPU_MAXNODES] = {0.0};
- #endif /* STARPU_USE_MIC */
- #ifdef STARPU_USE_MPI_MASTER_SLAVE
- static double mpi_time_device_to_device[STARPU_MAXMPIDEVS][STARPU_MAXMPIDEVS] = {{0.0}};
- static double mpi_latency_device_to_device[STARPU_MAXMPIDEVS][STARPU_MAXMPIDEVS] = {{0.0}};
- #endif
- #ifdef STARPU_HAVE_HWLOC
- static hwloc_topology_t hwtopology;
- hwloc_topology_t _starpu_perfmodel_get_hwtopology()
- {
- return hwtopology;
- }
- #endif
- #if (defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)) && !defined(STARPU_SIMGRID)
- #ifdef STARPU_USE_CUDA
- static void measure_bandwidth_between_host_and_dev_on_numa_with_cuda(int dev, int numa, int cpu, struct dev_timing *dev_timing_per_cpu)
- {
- struct _starpu_machine_config *config = _starpu_get_machine_config();
- _starpu_bind_thread_on_cpu(config, cpu, STARPU_NOWORKERID);
- size_t size = SIZE;
- const unsigned nnuma_nodes = _starpu_topology_get_nnumanodes(config);
- /* Initialize CUDA context on the device */
- /* We do not need to enable OpenGL interoperability at this point,
- * since we cleanly shutdown CUDA before returning. */
- cudaSetDevice(dev);
- /* hack to avoid third party libs to rebind threads */
- _starpu_bind_thread_on_cpu(config, cpu, STARPU_NOWORKERID);
- /* hack to force the initialization */
- cudaFree(0);
- /* hack to avoid third party libs to rebind threads */
- _starpu_bind_thread_on_cpu(config, cpu, STARPU_NOWORKERID);
- /* Get the maximum size which can be allocated on the device */
- struct cudaDeviceProp prop;
- cudaError_t cures;
- cures = cudaGetDeviceProperties(&prop, dev);
- if (STARPU_UNLIKELY(cures)) STARPU_CUDA_REPORT_ERROR(cures);
- cuda_size[dev] = prop.totalGlobalMem;
- if (size > prop.totalGlobalMem/4) size = prop.totalGlobalMem/4;
- /* Allocate a buffer on the device */
- unsigned char *d_buffer;
- cures = cudaMalloc((void **)&d_buffer, size);
- STARPU_ASSERT(cures == cudaSuccess);
- /* hack to avoid third party libs to rebind threads */
- _starpu_bind_thread_on_cpu(config, cpu, STARPU_NOWORKERID);
- /* Allocate a buffer on the host */
- unsigned char *h_buffer;
- #if defined(STARPU_HAVE_HWLOC)
- if (nnuma_nodes > 1)
- {
- /* NUMA mode activated */
- hwloc_obj_t obj = hwloc_get_obj_by_type(hwtopology, HWLOC_OBJ_NODE, numa);
- h_buffer = hwloc_alloc_membind_nodeset(hwtopology, size, obj->nodeset, HWLOC_MEMBIND_BIND, 0);
- }
- else
- #endif
- {
- /* we use STARPU_MAIN_RAM */
- _STARPU_MALLOC(h_buffer, size);
- cudaHostRegister((void *)h_buffer, size, 0);
- }
- STARPU_ASSERT(cures == cudaSuccess);
- /* hack to avoid third party libs to rebind threads */
- _starpu_bind_thread_on_cpu(config, cpu, STARPU_NOWORKERID);
- /* Fill them */
- memset(h_buffer, 0, size);
- cudaMemset(d_buffer, 0, size);
- /* hack to avoid third party libs to rebind threads */
- _starpu_bind_thread_on_cpu(config, cpu, STARPU_NOWORKERID);
- const unsigned timing_numa_index = dev*STARPU_MAXNUMANODES + numa;
- unsigned iter;
- double timing;
- double start;
- double end;
- /* Measure upload bandwidth */
- start = starpu_timing_now();
- for (iter = 0; iter < NITER; iter++)
- {
- cudaMemcpy(d_buffer, h_buffer, size, cudaMemcpyHostToDevice);
- cudaThreadSynchronize();
- }
- end = starpu_timing_now();
- timing = end - start;
- dev_timing_per_cpu[timing_numa_index].timing_htod = timing/NITER/size;
- /* Measure download bandwidth */
- start = starpu_timing_now();
- for (iter = 0; iter < NITER; iter++)
- {
- cudaMemcpy(h_buffer, d_buffer, size, cudaMemcpyDeviceToHost);
- cudaThreadSynchronize();
- }
- end = starpu_timing_now();
- timing = end - start;
- dev_timing_per_cpu[timing_numa_index].timing_dtoh = timing/NITER/size;
- /* Measure upload latency */
- start = starpu_timing_now();
- for (iter = 0; iter < NITER; iter++)
- {
- cudaMemcpy(d_buffer, h_buffer, 1, cudaMemcpyHostToDevice);
- cudaThreadSynchronize();
- }
- end = starpu_timing_now();
- timing = end - start;
- dev_timing_per_cpu[timing_numa_index].latency_htod = timing/NITER;
- /* Measure download latency */
- start = starpu_timing_now();
- for (iter = 0; iter < NITER; iter++)
- {
- cudaMemcpy(h_buffer, d_buffer, 1, cudaMemcpyDeviceToHost);
- cudaThreadSynchronize();
- }
- end = starpu_timing_now();
- timing = end - start;
- dev_timing_per_cpu[timing_numa_index].latency_dtoh = timing/NITER;
- /* Free buffers */
- cudaHostUnregister(h_buffer);
- #if defined(STARPU_HAVE_HWLOC)
- if (nnuma_nodes > 1)
- {
- /* NUMA mode activated */
- hwloc_free(hwtopology, h_buffer, size);
- }
- else
- #endif
- {
- free(h_buffer);
- }
- cudaFree(d_buffer);
- cudaThreadExit();
- }
- #ifdef HAVE_CUDA_MEMCPY_PEER
- static void measure_bandwidth_between_dev_and_dev_cuda(int src, int dst)
- {
- size_t size = SIZE;
- int can;
- /* Get the maximum size which can be allocated on the device */
- struct cudaDeviceProp prop;
- cudaError_t cures;
- cures = cudaGetDeviceProperties(&prop, src);
- if (STARPU_UNLIKELY(cures)) STARPU_CUDA_REPORT_ERROR(cures);
- if (size > prop.totalGlobalMem/4) size = prop.totalGlobalMem/4;
- cures = cudaGetDeviceProperties(&prop, dst);
- if (STARPU_UNLIKELY(cures)) STARPU_CUDA_REPORT_ERROR(cures);
- if (size > prop.totalGlobalMem/4) size = prop.totalGlobalMem/4;
- /* Initialize CUDA context on the source */
- /* We do not need to enable OpenGL interoperability at this point,
- * since we cleanly shutdown CUDA before returning. */
- cudaSetDevice(src);
- if (starpu_get_env_number("STARPU_ENABLE_CUDA_GPU_GPU_DIRECT") != 0)
- {
- cures = cudaDeviceCanAccessPeer(&can, src, dst);
- if (!cures && can)
- {
- cures = cudaDeviceEnablePeerAccess(dst, 0);
- if (!cures)
- {
- _STARPU_DISP("GPU-Direct %d -> %d\n", dst, src);
- cudadev_direct[src][dst] = 1;
- }
- }
- }
- /* Allocate a buffer on the device */
- unsigned char *s_buffer;
- cures = cudaMalloc((void **)&s_buffer, size);
- STARPU_ASSERT(cures == cudaSuccess);
- cudaMemset(s_buffer, 0, size);
- /* Initialize CUDA context on the destination */
- /* We do not need to enable OpenGL interoperability at this point,
- * since we cleanly shutdown CUDA before returning. */
- cudaSetDevice(dst);
- if (starpu_get_env_number("STARPU_ENABLE_CUDA_GPU_GPU_DIRECT") != 0)
- {
- cures = cudaDeviceCanAccessPeer(&can, dst, src);
- if (!cures && can)
- {
- cures = cudaDeviceEnablePeerAccess(src, 0);
- if (!cures)
- {
- _STARPU_DISP("GPU-Direct %d -> %d\n", src, dst);
- cudadev_direct[dst][src] = 1;
- }
- }
- }
- /* Allocate a buffer on the device */
- unsigned char *d_buffer;
- cures = cudaMalloc((void **)&d_buffer, size);
- STARPU_ASSERT(cures == cudaSuccess);
- cudaMemset(d_buffer, 0, size);
- unsigned iter;
- double timing;
- double start;
- double end;
- /* Measure upload bandwidth */
- start = starpu_timing_now();
- for (iter = 0; iter < NITER; iter++)
- {
- cudaMemcpyPeer(d_buffer, dst, s_buffer, src, size);
- cudaThreadSynchronize();
- }
- end = starpu_timing_now();
- timing = end - start;
- cudadev_timing_dtod[src][dst] = timing/NITER/size;
- /* Measure upload latency */
- start = starpu_timing_now();
- for (iter = 0; iter < NITER; iter++)
- {
- cudaMemcpyPeer(d_buffer, dst, s_buffer, src, 1);
- cudaThreadSynchronize();
- }
- end = starpu_timing_now();
- timing = end - start;
- cudadev_latency_dtod[src][dst] = timing/NITER;
- /* Free buffers */
- cudaFree(d_buffer);
- cudaSetDevice(src);
- cudaFree(s_buffer);
- cudaThreadExit();
- }
- #endif
- #endif
- #ifdef STARPU_USE_OPENCL
- static void measure_bandwidth_between_host_and_dev_on_numa_with_opencl(int dev, int numa, int cpu, struct dev_timing *dev_timing_per_cpu)
- {
- cl_context context;
- cl_command_queue queue;
- cl_int err=0;
- size_t size = SIZE;
- int not_initialized;
- struct _starpu_machine_config *config = _starpu_get_machine_config();
- _starpu_bind_thread_on_cpu(config, cpu, STARPU_NOWORKERID);
- const unsigned nnuma_nodes = _starpu_topology_get_nnumanodes(config);
- /* Is the context already initialised ? */
- starpu_opencl_get_context(dev, &context);
- not_initialized = (context == NULL);
- if (not_initialized == 1)
- _starpu_opencl_init_context(dev);
- /* Get context and queue */
- starpu_opencl_get_context(dev, &context);
- starpu_opencl_get_queue(dev, &queue);
- /* Get the maximum size which can be allocated on the device */
- cl_device_id device;
- cl_ulong maxMemAllocSize, totalGlobalMem;
- starpu_opencl_get_device(dev, &device);
- err = clGetDeviceInfo(device, CL_DEVICE_MAX_MEM_ALLOC_SIZE, sizeof(maxMemAllocSize), &maxMemAllocSize, NULL);
- if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
- if (size > (size_t)maxMemAllocSize/4) size = maxMemAllocSize/4;
- err = clGetDeviceInfo(device, CL_DEVICE_GLOBAL_MEM_SIZE , sizeof(totalGlobalMem), &totalGlobalMem, NULL);
- if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
- opencl_size[dev] = totalGlobalMem;
- if (_starpu_opencl_get_device_type(dev) == CL_DEVICE_TYPE_CPU)
- {
- /* Let's not use too much RAM when running OpenCL on a CPU: it
- * would make the OS swap like crazy. */
- size /= 2;
- }
- /* hack to avoid third party libs to rebind threads */
- _starpu_bind_thread_on_cpu(config, cpu, STARPU_NOWORKERID);
- /* Allocate a buffer on the device */
- cl_mem d_buffer;
- d_buffer = clCreateBuffer(context, CL_MEM_READ_WRITE, size, NULL, &err);
- if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
- /* hack to avoid third party libs to rebind threads */
- _starpu_bind_thread_on_cpu(config, cpu, STARPU_NOWORKERID);
- /* Allocate a buffer on the host */
- unsigned char *h_buffer;
- #if defined(STARPU_HAVE_HWLOC)
- if (nnuma_nodes > 1)
- {
- /* NUMA mode activated */
- hwloc_obj_t obj = hwloc_get_obj_by_type(hwtopology, HWLOC_OBJ_NODE, numa);
- h_buffer = hwloc_alloc_membind_nodeset(hwtopology, size, obj->nodeset, HWLOC_MEMBIND_BIND, 0);
- }
- else
- #endif
- {
- /* we use STARPU_MAIN_RAM */
- _STARPU_MALLOC(h_buffer, size);
- }
- /* hack to avoid third party libs to rebind threads */
- _starpu_bind_thread_on_cpu(config, cpu, STARPU_NOWORKERID);
- /* Fill them */
- memset(h_buffer, 0, size);
- err = clEnqueueWriteBuffer(queue, d_buffer, CL_TRUE, 0, size, h_buffer, 0, NULL, NULL);
- if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
- clFinish(queue);
- /* hack to avoid third party libs to rebind threads */
- _starpu_bind_thread_on_cpu(config, cpu, STARPU_NOWORKERID);
- const unsigned timing_numa_index = dev*STARPU_MAXNUMANODES + numa;
- unsigned iter;
- double timing;
- double start;
- double end;
- /* Measure upload bandwidth */
- start = starpu_timing_now();
- for (iter = 0; iter < NITER; iter++)
- {
- err = clEnqueueWriteBuffer(queue, d_buffer, CL_TRUE, 0, size, h_buffer, 0, NULL, NULL);
- if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
- clFinish(queue);
- }
- end = starpu_timing_now();
- timing = end - start;
- dev_timing_per_cpu[timing_numa_index].timing_htod = timing/NITER/size;
- /* Measure download bandwidth */
- start = starpu_timing_now();
- for (iter = 0; iter < NITER; iter++)
- {
- err = clEnqueueReadBuffer(queue, d_buffer, CL_TRUE, 0, size, h_buffer, 0, NULL, NULL);
- if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
- clFinish(queue);
- }
- end = starpu_timing_now();
- timing = end - start;
- dev_timing_per_cpu[timing_numa_index].timing_dtoh = timing/NITER/size;
- /* Measure upload latency */
- start = starpu_timing_now();
- for (iter = 0; iter < NITER; iter++)
- {
- err = clEnqueueWriteBuffer(queue, d_buffer, CL_TRUE, 0, 1, h_buffer, 0, NULL, NULL);
- if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
- clFinish(queue);
- }
- end = starpu_timing_now();
- timing = end - start;
- dev_timing_per_cpu[timing_numa_index].latency_htod = timing/NITER;
- /* Measure download latency */
- start = starpu_timing_now();
- for (iter = 0; iter < NITER; iter++)
- {
- err = clEnqueueReadBuffer(queue, d_buffer, CL_TRUE, 0, 1, h_buffer, 0, NULL, NULL);
- if (STARPU_UNLIKELY(err != CL_SUCCESS)) STARPU_OPENCL_REPORT_ERROR(err);
- clFinish(queue);
- }
- end = starpu_timing_now();
- timing = end - start;
- dev_timing_per_cpu[timing_numa_index].latency_dtoh = timing/NITER;
- /* Free buffers */
- err = clReleaseMemObject(d_buffer);
- if (STARPU_UNLIKELY(err != CL_SUCCESS))
- STARPU_OPENCL_REPORT_ERROR(err);
- #if defined(STARPU_HAVE_HWLOC)
- if (nnuma_nodes > 1)
- {
- /* NUMA mode activated */
- hwloc_free(hwtopology, h_buffer, size);
- }
- else
- #endif
- {
- free(h_buffer);
- }
- /* Uninitiliaze OpenCL context on the device */
- if (not_initialized == 1)
- _starpu_opencl_deinit_context(dev);
- }
- #endif
- /* NB: we want to sort the bandwidth by DECREASING order */
- static int compar_dev_timing(const void *left_dev_timing, const void *right_dev_timing)
- {
- const struct dev_timing *left = (const struct dev_timing *)left_dev_timing;
- const struct dev_timing *right = (const struct dev_timing *)right_dev_timing;
- double left_dtoh = left->timing_dtoh;
- double left_htod = left->timing_htod;
- double right_dtoh = right->timing_dtoh;
- double right_htod = right->timing_htod;
- double timing_sum2_left = left_dtoh*left_dtoh + left_htod*left_htod;
- double timing_sum2_right = right_dtoh*right_dtoh + right_htod*right_htod;
- /* it's for a decreasing sorting */
- return (timing_sum2_left > timing_sum2_right);
- }
- #ifdef STARPU_HAVE_HWLOC
- #if 0
- static int find_numa_node(hwloc_obj_t obj)
- {
- STARPU_ASSERT(obj);
- hwloc_obj_t current = obj;
- while (current->depth != HWLOC_OBJ_NODE)
- {
- current = current->parent;
- /* If we don't find a "node" obj before the root, this means
- * hwloc does not know whether there are numa nodes or not, so
- * we should not use a per-node sampling in that case. */
- STARPU_ASSERT(current);
- }
- STARPU_ASSERT(current->depth == HWLOC_OBJ_NODE);
- return current->logical_index;
- }
- #endif
- static int find_cpu_from_numa_node(hwloc_obj_t obj)
- {
- STARPU_ASSERT(obj);
- hwloc_obj_t current = obj;
- while (current->depth != HWLOC_OBJ_PU)
- {
- current = current->first_child;
- /* If we don't find a "PU" obj before the leave, this means
- * hwloc does not know whether there are CPU or not. */
- STARPU_ASSERT(current);
- }
- STARPU_ASSERT(current->depth == HWLOC_OBJ_PU);
- return current->logical_index;
- }
- #endif
- static void measure_bandwidth_between_numa_nodes_and_dev(int dev, struct dev_timing *dev_timing_per_numanode, char *type)
- {
- /* We measure the bandwith between each GPU and each NUMA node */
- struct _starpu_machine_config * config = _starpu_get_machine_config();
- const unsigned nnuma_nodes = _starpu_topology_get_nnumanodes(config);
- unsigned numa_id;
- for (numa_id = 0; numa_id < nnuma_nodes; numa_id++)
- {
- /* Store results by starpu id */
- const unsigned timing_numa_index = dev*STARPU_MAXNUMANODES + numa_id;
- /* Store STARPU_memnode for later */
- dev_timing_per_numanode[timing_numa_index].numa_id = numa_id;
- /* Chose one CPU connected to this NUMA node */
- unsigned cpu_id = 0;
- #ifdef STARPU_HAVE_HWLOC
- hwloc_obj_t obj = hwloc_get_obj_by_type(hwtopology, HWLOC_OBJ_NODE, numa_id);
- cpu_id = find_cpu_from_numa_node(obj);
- #endif
- #ifdef STARPU_USE_CUDA
- if (strncmp(type, "CUDA", 4) == 0)
- measure_bandwidth_between_host_and_dev_on_numa_with_cuda(dev, numa_id, cpu_id, dev_timing_per_numanode);
- #endif
- #ifdef STARPU_USE_OPENCL
- if (strncmp(type, "OpenCL", 6) == 0)
- measure_bandwidth_between_host_and_dev_on_numa_with_opencl(dev, numa_id, cpu_id, dev_timing_per_numanode);
- #endif
- }
- }
- static void measure_bandwidth_between_host_and_dev(int dev, struct dev_timing *dev_timing_per_numa, char *type)
- {
- measure_bandwidth_between_numa_nodes_and_dev(dev, dev_timing_per_numa, type);
- #ifdef STARPU_VERBOSE
- struct _starpu_machine_config * config = _starpu_get_machine_config();
- const unsigned nnuma_nodes = _starpu_topology_get_nnumanodes(config);
- unsigned numa_id;
- for (numa_id = 0; numa_id < nnuma_nodes; numa_id++)
- {
- const unsigned timing_numa_index = dev*STARPU_MAXNUMANODES + numa_id;
- double bandwidth_dtoh = dev_timing_per_numa[timing_numa_index].timing_dtoh;
- double bandwidth_htod = dev_timing_per_numa[timing_numa_index].timing_htod;
- double bandwidth_sum2 = bandwidth_dtoh*bandwidth_dtoh + bandwidth_htod*bandwidth_htod;
- _STARPU_DISP("(%10s) BANDWIDTH GPU %d NUMA %u - htod %f - dtoh %f - %f\n", type, dev, numa_id, bandwidth_htod, bandwidth_dtoh, sqrt(bandwidth_sum2));
- }
- #endif
- }
- #endif /* defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL) */
- static void measure_bandwidth_latency_between_numa(int numa_src, int numa_dst)
- {
- #if defined(STARPU_HAVE_HWLOC)
- if (nnumas > 1)
- {
- /* NUMA mode activated */
- double start, end, timing;
- unsigned iter;
- unsigned char *h_buffer;
- hwloc_obj_t obj_src = hwloc_get_obj_by_type(hwtopology, HWLOC_OBJ_NODE, numa_src);
- h_buffer = hwloc_alloc_membind_nodeset(hwtopology, SIZE, obj_src->nodeset, HWLOC_MEMBIND_BIND, 0);
- unsigned char *d_buffer;
- hwloc_obj_t obj_dst = hwloc_get_obj_by_type(hwtopology, HWLOC_OBJ_NODE, numa_dst);
- d_buffer = hwloc_alloc_membind_nodeset(hwtopology, SIZE, obj_dst->nodeset, HWLOC_MEMBIND_BIND, 0);
- memset(h_buffer, 0, SIZE);
- start = starpu_timing_now();
- for (iter = 0; iter < NITER; iter++)
- {
- memcpy(d_buffer, h_buffer, SIZE);
- }
- end = starpu_timing_now();
- timing = end - start;
- numa_timing[numa_src][numa_dst] = timing/NITER/SIZE;
- start = starpu_timing_now();
- for (iter = 0; iter < NITER; iter++)
- {
- memcpy(d_buffer, h_buffer, 1);
- }
- end = starpu_timing_now();
- timing = end - start;
- numa_latency[numa_src][numa_dst] = timing/NITER;
- hwloc_free(hwtopology, h_buffer, SIZE);
- hwloc_free(hwtopology, d_buffer, SIZE);
- }
- else
- #endif
- {
- /* Cannot make a real calibration */
- numa_timing[numa_src][numa_dst] = 0.01;
- numa_latency[numa_src][numa_dst] = 0;
- }
- }
- static void benchmark_all_gpu_devices(void)
- {
- #ifdef STARPU_SIMGRID
- _STARPU_DISP("Can not measure bus in simgrid mode, please run starpu_calibrate_bus in non-simgrid mode to make sure the bus performance model was calibrated\n");
- STARPU_ABORT();
- #else /* !SIMGRID */
- unsigned i, j;
- _STARPU_DEBUG("Benchmarking the speed of the bus\n");
- #ifdef STARPU_HAVE_HWLOC
- hwloc_topology_init(&hwtopology);
- hwloc_topology_load(hwtopology);
- #endif
- #ifdef STARPU_HAVE_HWLOC
- hwloc_bitmap_t former_cpuset = hwloc_bitmap_alloc();
- hwloc_get_cpubind(hwtopology, former_cpuset, HWLOC_CPUBIND_THREAD);
- #elif __linux__
- /* Save the current cpu binding */
- cpu_set_t former_process_affinity;
- int ret;
- ret = sched_getaffinity(0, sizeof(former_process_affinity), &former_process_affinity);
- if (ret)
- {
- perror("sched_getaffinity");
- STARPU_ABORT();
- }
- #else
- #warning Missing binding support, StarPU will not be able to properly benchmark NUMA topology
- #endif
- struct _starpu_machine_config *config = _starpu_get_machine_config();
- ncpus = _starpu_topology_get_nhwcpu(config);
- nnumas = _starpu_topology_get_nnumanodes(config);
- for (i = 0; i < nnumas; i++)
- for (j = 0; j < nnumas; j++)
- if (i != j)
- {
- _STARPU_DISP("NUMA %d -> %d...\n", i, j);
- measure_bandwidth_latency_between_numa(i, j);
- }
- #ifdef STARPU_USE_CUDA
- ncuda = _starpu_get_cuda_device_count();
- for (i = 0; i < ncuda; i++)
- {
- _STARPU_DISP("CUDA %u...\n", i);
- /* measure bandwidth between Host and Device i */
- measure_bandwidth_between_host_and_dev(i, cudadev_timing_per_numa, "CUDA");
- }
- #ifdef HAVE_CUDA_MEMCPY_PEER
- for (i = 0; i < ncuda; i++)
- {
- unsigned j;
- for (j = 0; j < ncuda; j++)
- if (i != j)
- {
- _STARPU_DISP("CUDA %u -> %u...\n", i, j);
- /* measure bandwidth between Host and Device i */
- measure_bandwidth_between_dev_and_dev_cuda(i, j);
- }
- }
- #endif
- #endif
- #ifdef STARPU_USE_OPENCL
- nopencl = _starpu_opencl_get_device_count();
- for (i = 0; i < nopencl; i++)
- {
- _STARPU_DISP("OpenCL %u...\n", i);
- /* measure bandwith between Host and Device i */
- measure_bandwidth_between_host_and_dev(i, opencldev_timing_per_numa, "OpenCL");
- }
- #endif
- #ifdef STARPU_USE_MIC
- /* TODO: implement real calibration ! For now we only put an arbitrary
- * value for each device during at the declaration as a bug fix, else
- * we get problems on heft scheduler */
- nmic = _starpu_mic_src_get_device_count();
- for (i = 0; i < STARPU_MAXNODES; i++)
- {
- mic_time_host_to_device[i] = 0.1;
- mic_time_device_to_host[i] = 0.1;
- }
- #endif /* STARPU_USE_MIC */
- #ifdef STARPU_USE_MPI_MASTER_SLAVE
- _starpu_mpi_common_measure_bandwidth_latency(mpi_time_device_to_device, mpi_latency_device_to_device);
- #endif /* STARPU_USE_MPI_MASTER_SLAVE */
- #ifdef STARPU_HAVE_HWLOC
- hwloc_set_cpubind(hwtopology, former_cpuset, HWLOC_CPUBIND_THREAD);
- hwloc_bitmap_free(former_cpuset);
- #elif __linux__
- /* Restore the former affinity */
- ret = sched_setaffinity(0, sizeof(former_process_affinity), &former_process_affinity);
- if (ret)
- {
- perror("sched_setaffinity");
- STARPU_ABORT();
- }
- #endif
- #ifdef STARPU_HAVE_HWLOC
- hwloc_topology_destroy(hwtopology);
- #endif
- _STARPU_DEBUG("Benchmarking the speed of the bus is done.\n");
- was_benchmarked = 1;
- #endif /* !SIMGRID */
- }
- static void get_bus_path(const char *type, char *path, size_t maxlen)
- {
- char hostname[65];
- _starpu_gethostname(hostname, sizeof(hostname));
- snprintf(path, maxlen, "%s%s.%s", _starpu_get_perf_model_dir_bus(), hostname, type);
- }
- /*
- * Affinity
- */
- static void get_affinity_path(char *path, size_t maxlen)
- {
- get_bus_path("affinity", path, maxlen);
- }
- #ifndef STARPU_SIMGRID
- static void load_bus_affinity_file_content(void)
- {
- #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)
- FILE *f;
- int locked;
- char path[256];
- get_affinity_path(path, sizeof(path));
- _STARPU_DEBUG("loading affinities from %s\n", path);
- f = fopen(path, "r");
- STARPU_ASSERT(f);
- locked = _starpu_frdlock(f) == 0;
- unsigned gpu;
- #ifdef STARPU_USE_CUDA
- ncuda = _starpu_get_cuda_device_count();
- for (gpu = 0; gpu < ncuda; gpu++)
- {
- int ret;
- unsigned dummy;
- _starpu_drop_comments(f);
- ret = fscanf(f, "%u\t", &dummy);
- STARPU_ASSERT(ret == 1);
- STARPU_ASSERT(dummy == gpu);
- unsigned numa;
- for (numa = 0; numa < nnumas; numa++)
- {
- ret = fscanf(f, "%d\t", &cuda_affinity_matrix[gpu][numa]);
- STARPU_ASSERT(ret == 1);
- }
- ret = fscanf(f, "\n");
- STARPU_ASSERT(ret == 0);
- }
- #endif /* !STARPU_USE_CUDA */
- #ifdef STARPU_USE_OPENCL
- nopencl = _starpu_opencl_get_device_count();
- for (gpu = 0; gpu < nopencl; gpu++)
- {
- int ret;
- unsigned dummy;
- _starpu_drop_comments(f);
- ret = fscanf(f, "%u\t", &dummy);
- STARPU_ASSERT(ret == 1);
- STARPU_ASSERT(dummy == gpu);
- unsigned numa;
- for (numa = 0; numa < nnumas; numa++)
- {
- ret = fscanf(f, "%d\t", &opencl_affinity_matrix[gpu][numa]);
- STARPU_ASSERT(ret == 1);
- }
- ret = fscanf(f, "\n");
- STARPU_ASSERT(ret == 0);
- }
- #endif /* !STARPU_USE_OPENCL */
- if (locked)
- _starpu_frdunlock(f);
- fclose(f);
- #endif /* !(STARPU_USE_CUDA_ || STARPU_USE_OPENCL */
- }
- #ifndef STARPU_SIMGRID
- static void write_bus_affinity_file_content(void)
- {
- STARPU_ASSERT(was_benchmarked);
- #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)
- FILE *f;
- char path[256];
- int locked;
- get_affinity_path(path, sizeof(path));
- _STARPU_DEBUG("writing affinities to %s\n", path);
- f = fopen(path, "w+");
- if (!f)
- {
- perror("fopen write_buf_affinity_file_content");
- _STARPU_DISP("path '%s'\n", path);
- fflush(stderr);
- STARPU_ABORT();
- }
- locked = _starpu_frdlock(f) == 0;
- unsigned numa;
- unsigned gpu;
- fprintf(f, "# GPU\t");
- for (numa = 0; numa < nnumas; numa++)
- fprintf(f, "NUMA%u\t", numa);
- fprintf(f, "\n");
- #ifdef STARPU_USE_CUDA
- {
- /* Use an other array to sort bandwidth */
- struct dev_timing cudadev_timing_per_numa_sorted[STARPU_MAXCUDADEVS*STARPU_MAXNUMANODES];
- memcpy(cudadev_timing_per_numa_sorted, cudadev_timing_per_numa, STARPU_MAXCUDADEVS*STARPU_MAXNUMANODES*sizeof(struct dev_timing));
- for (gpu = 0; gpu < ncuda; gpu++)
- {
- fprintf(f, "%u\t", gpu);
- qsort(&(cudadev_timing_per_numa_sorted[gpu*STARPU_MAXNUMANODES]), nnumas, sizeof(struct dev_timing), compar_dev_timing);
- for (numa = 0; numa < nnumas; numa++)
- {
- fprintf(f, "%d\t", cudadev_timing_per_numa_sorted[gpu*STARPU_MAXNUMANODES+numa].numa_id);
- }
- fprintf(f, "\n");
- }
- }
- #endif
- #ifdef STARPU_USE_OPENCL
- {
- /* Use an other array to sort bandwidth */
- struct dev_timing opencldev_timing_per_numa_sorted[STARPU_MAXOPENCLDEVS*STARPU_MAXNUMANODES];
- memcpy(opencldev_timing_per_numa_sorted, opencldev_timing_per_numa, STARPU_MAXOPENCLDEVS*STARPU_MAXNUMANODES*sizeof(struct dev_timing));
- for (gpu = 0; gpu < nopencl; gpu++)
- {
- fprintf(f, "%u\t", gpu);
- qsort(&(opencldev_timing_per_numa_sorted[gpu*STARPU_MAXNUMANODES]), nnumas, sizeof(struct dev_timing), compar_dev_timing);
- for (numa = 0; numa < nnumas; numa++)
- {
- fprintf(f, "%d\t", opencldev_timing_per_numa_sorted[gpu*STARPU_MAXNUMANODES+numa].numa_id);
- }
- fprintf(f, "\n");
- }
- }
- #endif
- if (locked)
- _starpu_frdunlock(f);
- fclose(f);
- #endif
- }
- #endif /* STARPU_SIMGRID */
- static void generate_bus_affinity_file(void)
- {
- if (!was_benchmarked)
- benchmark_all_gpu_devices();
- #ifdef STARPU_USE_MPI_MASTER_SLAVE
- /* Slaves don't write files */
- if (!_starpu_mpi_common_is_src_node())
- return;
- #endif
- write_bus_affinity_file_content();
- }
- static int check_bus_affinity_file(void)
- {
- int ret = 1;
- #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)
- FILE *f;
- int locked;
- unsigned dummy;
- char path[256];
- get_affinity_path(path, sizeof(path));
- _STARPU_DEBUG("loading affinities from %s\n", path);
- f = fopen(path, "r");
- STARPU_ASSERT(f);
- locked = _starpu_frdlock(f) == 0;
- ret = fscanf(f, "# GPU\t");
- STARPU_ASSERT(ret == 0);
- ret = fscanf(f, "NUMA%u\t", &dummy);
- if (locked)
- _starpu_frdunlock(f);
- fclose(f);
- #endif
- return ret == 1;
- }
- static void load_bus_affinity_file(void)
- {
- int exist, check = 1;
- char path[256];
- get_affinity_path(path, sizeof(path));
- /* access return 0 if file exists */
- exist = access(path, F_OK);
- if (exist == 0)
- /* return 0 if it's not good */
- check = check_bus_affinity_file();
- if (check == 0)
- _STARPU_DISP("Affinity File is too old for this version of StarPU ! Rebuilding it...\n");
- if (check == 0 || exist != 0)
- {
- /* File does not exist yet */
- generate_bus_affinity_file();
- }
- load_bus_affinity_file_content();
- }
- #ifdef STARPU_USE_CUDA
- int *_starpu_get_cuda_affinity_vector(unsigned gpuid)
- {
- return cuda_affinity_matrix[gpuid];
- }
- #endif /* STARPU_USE_CUDA */
- #ifdef STARPU_USE_OPENCL
- int *_starpu_get_opencl_affinity_vector(unsigned gpuid)
- {
- return opencl_affinity_matrix[gpuid];
- }
- #endif /* STARPU_USE_OPENCL */
- void starpu_bus_print_affinity(FILE *f)
- {
- #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)
- unsigned numa;
- unsigned gpu;
- #endif
- fprintf(f, "# GPU\tNUMA in preference order (logical index)\n");
- #ifdef STARPU_USE_CUDA
- fprintf(f, "# CUDA\n");
- for(gpu = 0 ; gpu<ncuda ; gpu++)
- {
- fprintf(f, "%u\t", gpu);
- for (numa = 0; numa < nnumas; numa++)
- {
- fprintf(f, "%d\t", cuda_affinity_matrix[gpu][numa]);
- }
- fprintf(f, "\n");
- }
- #endif
- #ifdef STARPU_USE_OPENCL
- fprintf(f, "# OpenCL\n");
- for(gpu = 0 ; gpu<nopencl ; gpu++)
- {
- fprintf(f, "%u\t", gpu);
- for (numa = 0; numa < nnumas; numa++)
- {
- fprintf(f, "%d\t", opencl_affinity_matrix[gpu][numa]);
- }
- fprintf(f, "\n");
- }
- #endif
- }
- #endif /* STARPU_SIMGRID */
- /*
- * Latency
- */
- static void get_latency_path(char *path, size_t maxlen)
- {
- get_bus_path("latency", path, maxlen);
- }
- static int load_bus_latency_file_content(void)
- {
- int n;
- unsigned src, dst;
- FILE *f;
- double latency;
- int locked;
- char path[256];
- get_latency_path(path, sizeof(path));
- _STARPU_DEBUG("loading latencies from %s\n", path);
- f = fopen(path, "r");
- if (!f)
- {
- perror("fopen load_bus_latency_file_content");
- _STARPU_DISP("path '%s'\n", path);
- fflush(stderr);
- STARPU_ABORT();
- }
- locked = _starpu_frdlock(f) == 0;
- for (src = 0; src < STARPU_MAXNODES; src++)
- {
- _starpu_drop_comments(f);
- for (dst = 0; dst < STARPU_MAXNODES; dst++)
- {
- n = _starpu_read_double(f, "%le", &latency);
- if (n != 1)
- {
- _STARPU_DISP("Error while reading latency file <%s>. Expected a number. Did you change the maximum number of GPUs at ./configure time?\n", path);
- fclose(f);
- return 0;
- }
- n = getc(f);
- if (n == '\n')
- break;
- if (n != '\t')
- {
- _STARPU_DISP("bogus character '%c' (%d) in latency file %s\n", n, n, path);
- fclose(f);
- return 0;
- }
- latency_matrix[src][dst] = latency;
- /* Look out for \t\n */
- n = getc(f);
- if (n == '\n')
- break;
- ungetc(n, f);
- n = '\t';
- }
- /* No more values, take NAN */
- for ( ; dst < STARPU_MAXNODES; dst++)
- latency_matrix[src][dst] = NAN;
- while (n == '\t')
- {
- /* Look out for \t\n */
- n = getc(f);
- if (n == '\n')
- break;
- ungetc(n, f);
- n = _starpu_read_double(f, "%le", &latency);
- if (n && !isnan(latency))
- {
- _STARPU_DISP("Too many nodes in latency file %s for this configuration (%d). Did you change the maximum number of GPUs at ./configure time?\n", path, STARPU_MAXNODES);
- fclose(f);
- return 0;
- }
- n = getc(f);
- }
- if (n != '\n')
- {
- _STARPU_DISP("Bogus character '%c' (%d) in latency file %s\n", n, n, path);
- fclose(f);
- return 0;
- }
- /* Look out for EOF */
- n = getc(f);
- if (n == EOF)
- break;
- ungetc(n, f);
- }
- if (locked)
- _starpu_frdunlock(f);
- fclose(f);
- /* No more values, take NAN */
- for ( ; src < STARPU_MAXNODES; src++)
- for (dst = 0; dst < STARPU_MAXNODES; dst++)
- latency_matrix[src][dst] = NAN;
- return 1;
- }
- #ifndef STARPU_SIMGRID
- static double search_bus_best_latency(int src, char * type, int htod)
- {
- /* Search the best latency for this node */
- double best = 0.0;
- double actual = 0.0;
- unsigned check = 0;
- unsigned numa;
- for (numa = 0; numa < nnumas; numa++)
- {
- #ifdef STARPU_USE_CUDA
- if (strncmp(type, "CUDA", 4) == 0)
- {
- if (htod)
- actual = cudadev_timing_per_numa[src*STARPU_MAXNUMANODES+numa].latency_htod;
- else
- actual = cudadev_timing_per_numa[src*STARPU_MAXNUMANODES+numa].latency_dtoh;
- }
- #endif
- #ifdef STARPU_USE_OPENCL
- if (strncmp(type, "OpenCL", 6) == 0)
- {
- if (htod)
- actual = opencldev_timing_per_numa[src*STARPU_MAXNUMANODES+numa].latency_htod;
- else
- actual = opencldev_timing_per_numa[src*STARPU_MAXNUMANODES+numa].latency_dtoh;
- }
- #endif
- #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)
- if (!check || actual < best)
- {
- best = actual;
- check = 1;
- }
- #endif
- }
- return best;
- }
- static void write_bus_latency_file_content(void)
- {
- unsigned src, dst, maxnode;
- /* Boundaries to check if src or dst are inside the interval */
- unsigned b_low, b_up;
- FILE *f;
- int locked;
- STARPU_ASSERT(was_benchmarked);
- char path[256];
- get_latency_path(path, sizeof(path));
- _STARPU_DEBUG("writing latencies to %s\n", path);
- f = fopen(path, "w+");
- if (!f)
- {
- perror("fopen write_bus_latency_file_content");
- _STARPU_DISP("path '%s'\n", path);
- fflush(stderr);
- STARPU_ABORT();
- }
- locked = _starpu_fwrlock(f) == 0;
- _starpu_fftruncate(f, 0);
- fprintf(f, "# ");
- for (dst = 0; dst < STARPU_MAXNODES; dst++)
- fprintf(f, "to %u\t\t", dst);
- fprintf(f, "\n");
- maxnode = nnumas;
- #ifdef STARPU_USE_CUDA
- maxnode += ncuda;
- #endif
- #ifdef STARPU_USE_OPENCL
- maxnode += nopencl;
- #endif
- #ifdef STARPU_USE_MIC
- maxnode += nmic;
- #endif
- #ifdef STARPU_USE_MPI_MASTER_SLAVE
- maxnode += nmpi_ms;
- #endif
- for (src = 0; src < STARPU_MAXNODES; src++)
- {
- for (dst = 0; dst < STARPU_MAXNODES; dst++)
- {
- /* µs */
- double latency = 0.0;
- if ((src >= maxnode) || (dst >= maxnode))
- {
- /* convention */
- latency = NAN;
- }
- else if (src == dst)
- {
- latency = 0.0;
- }
- else
- {
- b_low = b_up = 0;
- /* ---- Begin NUMA ---- */
- b_up += nnumas;
- if (src >= b_low && src < b_up && dst >= b_low && dst < b_up)
- latency += numa_latency[src-b_low][dst-b_low];
- /* copy interval to check numa index later */
- unsigned numa_low = b_low;
- unsigned numa_up = b_up;
- b_low += nnumas;
- /* ---- End NUMA ---- */
- #ifdef STARPU_USE_CUDA
- b_up += ncuda;
- #ifdef HAVE_CUDA_MEMCPY_PEER
- if (src >= b_low && src < b_up && dst >= b_low && dst < b_up)
- latency += cudadev_latency_dtod[src-b_low][dst-b_low];
- else
- #endif
- {
- /* Check if it's CUDA <-> NUMA link */
- if (src >=b_low && src < b_up && dst >= numa_low && dst < numa_up)
- latency += cudadev_timing_per_numa[(src-b_low)*STARPU_MAXNUMANODES+dst-numa_low].latency_dtoh;
- if (dst >= b_low && dst < b_up && src >= numa_low && dst < numa_up)
- latency += cudadev_timing_per_numa[(dst-b_low)*STARPU_MAXNUMANODES+src-numa_low].latency_htod;
- /* To other devices, take the best latency */
- if (src >= b_low && src < b_up && !(dst >= numa_low && dst < numa_up))
- latency += search_bus_best_latency(src-b_low, "CUDA", 0);
- if (dst >= b_low && dst < b_up && !(src >= numa_low && dst < numa_up))
- latency += search_bus_best_latency(dst-b_low, "CUDA", 1);
- }
- b_low += ncuda;
- #endif
- #ifdef STARPU_USE_OPENCL
- b_up += nopencl;
- /* Check if it's OpenCL <-> NUMA link */
- if (src >= b_low && src < b_up && dst >= numa_low && dst < numa_up)
- latency += opencldev_timing_per_numa[(src-b_low)*STARPU_MAXNUMANODES+dst-numa_low].latency_dtoh;
- if (dst >= b_low && dst < b_up && src >= numa_low && dst < numa_up)
- latency += opencldev_timing_per_numa[(dst-b_low)*STARPU_MAXNUMANODES+src-numa_low].latency_htod;
- /* To other devices, take the best latency */
- if (src >= b_low && src < b_up && !(dst >= numa_low && dst < numa_up))
- latency += search_bus_best_latency(src-b_low, "OpenCL", 0);
- if (dst >= b_low && dst < b_up && !(src >= numa_low && dst < numa_up))
- latency += search_bus_best_latency(dst-b_low, "OpenCL", 1);
- b_low += nopencl;
- #endif
- #ifdef STARPU_USE_MIC
- b_up += nmic;
- /* TODO Latency MIC */
- b_low += nmic;
- #endif
- #ifdef STARPU_USE_MPI_MASTER_SLAVE
- b_up += nmpi_ms;
- /* Modify MPI src and MPI dst if they contain the master node or not
- * Because, we only take care about slaves */
- int mpi_master = _starpu_mpi_common_get_src_node();
- int mpi_src = src - b_low;
- mpi_src = (mpi_master <= mpi_src) ? mpi_src+1 : mpi_src;
- int mpi_dst = dst - b_low;
- mpi_dst = (mpi_master <= mpi_dst) ? mpi_dst+1 : mpi_dst;
- if (src >= b_low && src < b_up && dst >= b_low && dst < b_up)
- latency += mpi_latency_device_to_device[mpi_src][mpi_dst];
- else
- {
- if (src >= b_low && src < b_up)
- latency += mpi_latency_device_to_device[mpi_src][mpi_master];
- if (dst >= b_low && dst < b_up)
- latency += mpi_latency_device_to_device[mpi_master][mpi_dst];
- }
- b_low += nmpi_ms;
- #endif
- }
- if (dst > 0)
- fputc('\t', f);
- _starpu_write_double(f, "%e", latency);
- }
- fprintf(f, "\n");
- }
- if (locked)
- _starpu_fwrunlock(f);
- fclose(f);
- }
- #endif
- static void generate_bus_latency_file(void)
- {
- if (!was_benchmarked)
- benchmark_all_gpu_devices();
- #ifdef STARPU_USE_MPI_MASTER_SLAVE
- /* Slaves don't write files */
- if (!_starpu_mpi_common_is_src_node())
- return;
- #endif
- #ifndef STARPU_SIMGRID
- write_bus_latency_file_content();
- #endif
- }
- static void load_bus_latency_file(void)
- {
- int res;
- char path[256];
- get_latency_path(path, sizeof(path));
- res = access(path, F_OK);
- if (res || !load_bus_latency_file_content())
- {
- /* File does not exist yet or is bogus */
- generate_bus_latency_file();
- }
- }
- /*
- * Bandwidth
- */
- static void get_bandwidth_path(char *path, size_t maxlen)
- {
- get_bus_path("bandwidth", path, maxlen);
- }
- static int load_bus_bandwidth_file_content(void)
- {
- int n;
- unsigned src, dst;
- FILE *f;
- double bandwidth;
- int locked;
- char path[256];
- get_bandwidth_path(path, sizeof(path));
- _STARPU_DEBUG("loading bandwidth from %s\n", path);
- f = fopen(path, "r");
- if (!f)
- {
- perror("fopen load_bus_bandwidth_file_content");
- _STARPU_DISP("path '%s'\n", path);
- fflush(stderr);
- STARPU_ABORT();
- }
- locked = _starpu_frdlock(f) == 0;
- for (src = 0; src < STARPU_MAXNODES; src++)
- {
- _starpu_drop_comments(f);
- for (dst = 0; dst < STARPU_MAXNODES; dst++)
- {
- n = _starpu_read_double(f, "%le", &bandwidth);
- if (n != 1)
- {
- _STARPU_DISP("Error while reading bandwidth file <%s>. Expected a number\n", path);
- fclose(f);
- return 0;
- }
- n = getc(f);
- if (n == '\n')
- break;
- if (n != '\t')
- {
- _STARPU_DISP("bogus character '%c' (%d) in bandwidth file %s\n", n, n, path);
- fclose(f);
- return 0;
- }
- bandwidth_matrix[src][dst] = bandwidth;
- /* Look out for \t\n */
- n = getc(f);
- if (n == '\n')
- break;
- ungetc(n, f);
- n = '\t';
- }
- /* No more values, take NAN */
- for ( ; dst < STARPU_MAXNODES; dst++)
- bandwidth_matrix[src][dst] = NAN;
- while (n == '\t')
- {
- /* Look out for \t\n */
- n = getc(f);
- if (n == '\n')
- break;
- ungetc(n, f);
- n = _starpu_read_double(f, "%le", &bandwidth);
- if (n && !isnan(bandwidth))
- {
- _STARPU_DISP("Too many nodes in bandwidth file %s for this configuration (%d)\n", path, STARPU_MAXNODES);
- fclose(f);
- return 0;
- }
- n = getc(f);
- }
- if (n != '\n')
- {
- _STARPU_DISP("Bogus character '%c' (%d) in bandwidth file %s\n", n, n, path);
- fclose(f);
- return 0;
- }
- /* Look out for EOF */
- n = getc(f);
- if (n == EOF)
- break;
- ungetc(n, f);
- }
- if (locked)
- _starpu_frdunlock(f);
- fclose(f);
- /* No more values, take NAN */
- for ( ; src < STARPU_MAXNODES; src++)
- for (dst = 0; dst < STARPU_MAXNODES; dst++)
- latency_matrix[src][dst] = NAN;
- return 1;
- }
- #ifndef STARPU_SIMGRID
- static double search_bus_best_timing(int src, char * type, int htod)
- {
- /* Search the best latency for this node */
- double best = 0.0;
- double actual = 0.0;
- unsigned check = 0;
- unsigned numa;
- for (numa = 0; numa < nnumas; numa++)
- {
- #ifdef STARPU_USE_CUDA
- if (strncmp(type, "CUDA", 4) == 0)
- {
- if (htod)
- actual = cudadev_timing_per_numa[src*STARPU_MAXNUMANODES+numa].timing_htod;
- else
- actual = cudadev_timing_per_numa[src*STARPU_MAXNUMANODES+numa].timing_dtoh;
- }
- #endif
- #ifdef STARPU_USE_OPENCL
- if (strncmp(type, "OpenCL", 6) == 0)
- {
- if (htod)
- actual = opencldev_timing_per_numa[src*STARPU_MAXNUMANODES+numa].timing_htod;
- else
- actual = opencldev_timing_per_numa[src*STARPU_MAXNUMANODES+numa].timing_dtoh;
- }
- #endif
- #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)
- if (!check || actual < best)
- {
- best = actual;
- check = 1;
- }
- #endif
- }
- return best;
- }
- static void write_bus_bandwidth_file_content(void)
- {
- unsigned src, dst, maxnode;
- unsigned b_low, b_up;
- FILE *f;
- int locked;
- STARPU_ASSERT(was_benchmarked);
- char path[256];
- get_bandwidth_path(path, sizeof(path));
- _STARPU_DEBUG("writing bandwidth to %s\n", path);
- f = fopen(path, "w+");
- STARPU_ASSERT(f);
- locked = _starpu_fwrlock(f) == 0;
- _starpu_fftruncate(f, 0);
- fprintf(f, "# ");
- for (dst = 0; dst < STARPU_MAXNODES; dst++)
- fprintf(f, "to %u\t\t", dst);
- fprintf(f, "\n");
- maxnode = nnumas;
- #ifdef STARPU_USE_CUDA
- maxnode += ncuda;
- #endif
- #ifdef STARPU_USE_OPENCL
- maxnode += nopencl;
- #endif
- #ifdef STARPU_USE_MIC
- maxnode += nmic;
- #endif
- #ifdef STARPU_USE_MPI_MASTER_SLAVE
- maxnode += nmpi_ms;
- #endif
- for (src = 0; src < STARPU_MAXNODES; src++)
- {
- for (dst = 0; dst < STARPU_MAXNODES; dst++)
- {
- double bandwidth;
- if ((src >= maxnode) || (dst >= maxnode))
- {
- bandwidth = NAN;
- }
- else if (src != dst)
- {
- double slowness = 0.0;
- /* Total bandwidth is the harmonic mean of bandwidths */
- b_low = b_up = 0;
- /* Begin NUMA */
- b_up += nnumas;
- if (src >= b_low && src < b_up && dst >= b_low && dst < b_up)
- slowness += numa_timing[src-b_low][dst-b_low];
- /* copy interval to check numa index later */
- unsigned numa_low = b_low;
- unsigned numa_up = b_up;
- b_low += nnumas;
- /* End NUMA */
- #ifdef STARPU_USE_CUDA
- b_up += ncuda;
- #ifdef HAVE_CUDA_MEMCPY_PEER
- if (src >= b_low && src < b_up && dst >= b_low && dst < b_up)
- /* Direct GPU-GPU transfert */
- slowness += cudadev_timing_dtod[src-b_low][dst-b_low];
- else
- #endif
- {
- /* Check if it's CUDA <-> NUMA link */
- if (src >= b_low && src < b_up && dst >= numa_low && dst < numa_up)
- slowness += cudadev_timing_per_numa[(src-b_low)*STARPU_MAXNUMANODES+dst-numa_low].timing_dtoh;
- if (dst >= b_low && dst < b_up && src >= numa_low && dst < numa_up)
- slowness += cudadev_timing_per_numa[(dst-b_low)*STARPU_MAXNUMANODES+src-numa_low].timing_htod;
- /* To other devices, take the best slowness */
- if (src >= b_low && src < b_up && !(dst >= numa_low && dst < numa_up))
- slowness += search_bus_best_timing(src-b_low, "CUDA", 0);
- if (dst >= b_low && dst < b_up && !(src >= numa_low && dst < numa_up))
- slowness += search_bus_best_timing(dst-b_low, "CUDA", 1);
- }
- b_low += ncuda;
- #endif
- #ifdef STARPU_USE_OPENCL
- b_up += nopencl;
- /* Check if it's OpenCL <-> NUMA link */
- if (src >= b_low && src < b_up && dst >= numa_low && dst < numa_up)
- slowness += opencldev_timing_per_numa[(src-b_low)*STARPU_MAXNUMANODES+dst-numa_low].timing_dtoh;
- if (dst >= b_low && dst < b_up && src >= numa_low && dst < numa_up)
- slowness += opencldev_timing_per_numa[(dst-b_low)*STARPU_MAXNUMANODES+src-numa_low].timing_htod;
- /* To other devices, take the best slowness */
- if (src >= b_low && src < b_up && !(dst >= numa_low && dst < numa_up))
- slowness += search_bus_best_timing(src-b_low, "OpenCL", 0);
- if (dst >= b_low && dst < b_up && !(src >= numa_low && dst < numa_up))
- slowness += search_bus_best_timing(dst-b_low, "OpenCL", 1);
- b_low += nopencl;
- #endif
- #ifdef STARPU_USE_MIC
- b_up += nmic;
- if (src >= b_low && src < b_up)
- slowness += mic_time_device_to_host[src-b_low];
- if (dst >= b_low && dst < b_up)
- slowness += mic_time_host_to_device[dst-b_low];
- b_low += nmic;
- #endif
- #ifdef STARPU_USE_MPI_MASTER_SLAVE
- b_up += nmpi_ms;
- /* Modify MPI src and MPI dst if they contain the master node or not
- * Because, we only take care about slaves */
- int mpi_master = _starpu_mpi_common_get_src_node();
- int mpi_src = src - b_low;
- mpi_src = (mpi_master <= mpi_src) ? mpi_src+1 : mpi_src;
- int mpi_dst = dst - b_low;
- mpi_dst = (mpi_master <= mpi_dst) ? mpi_dst+1 : mpi_dst;
- if (src >= b_low && src < b_up && dst >= b_low && dst < b_up)
- slowness += mpi_time_device_to_device[mpi_src][mpi_dst];
- else
- {
- if (src >= b_low && src < b_up)
- slowness += mpi_time_device_to_device[mpi_src][mpi_master];
- if (dst >= b_low && dst < b_up)
- slowness += mpi_time_device_to_device[mpi_master][mpi_dst];
- }
- b_low += nmpi_ms;
- #endif
- bandwidth = 1.0/slowness;
- }
- else
- {
- /* convention */
- bandwidth = 0.0;
- }
- if (dst)
- fputc('\t', f);
- _starpu_write_double(f, "%e", bandwidth);
- }
- fprintf(f, "\n");
- }
- if (locked)
- _starpu_fwrunlock(f);
- fclose(f);
- }
- #endif /* STARPU_SIMGRID */
- void starpu_bus_print_filenames(FILE *output)
- {
- char bandwidth_path[256];
- char affinity_path[256];
- char latency_path[256];
- get_bandwidth_path(bandwidth_path, sizeof(bandwidth_path));
- get_affinity_path(affinity_path, sizeof(affinity_path));
- get_latency_path(latency_path, sizeof(latency_path));
- fprintf(output, "bandwidth: <%s>\n", bandwidth_path);
- fprintf(output, " affinity: <%s>\n", affinity_path);
- fprintf(output, " latency: <%s>\n", latency_path);
- }
- void starpu_bus_print_bandwidth(FILE *f)
- {
- unsigned src, dst, maxnode;
- maxnode = nnumas;
- #ifdef STARPU_USE_CUDA
- maxnode += ncuda;
- #endif
- #ifdef STARPU_USE_OPENCL
- maxnode += nopencl;
- #endif
- #ifdef STARPU_USE_MIC
- maxnode += nmic;
- #endif
- #ifdef STARPU_USE_MPI_MASTER_SLAVE
- maxnode += nmpi_ms;
- #endif
- fprintf(f, "from/to\t");
- for (dst = 0; dst < nnumas; dst++)
- fprintf(f, "NUMA_%u\t", dst);
- for (dst = 0; dst < ncuda; dst++)
- fprintf(f, "CUDA_%u\t", dst);
- for (dst = 0; dst < nopencl; dst++)
- fprintf(f, "OpenCL%u\t", dst);
- for (dst = 0; dst < nmic; dst++)
- fprintf(f, "MIC_%u\t", dst);
- for (dst = 0; dst < nmpi_ms; dst++)
- fprintf(f, "MPI_MS%u\t", dst);
- fprintf(f, "\n");
- for (src = 0; src < maxnode; src++)
- {
- if (src < nnumas)
- fprintf(f, "NUMA_%u\t", src);
- else if (src < nnumas + ncuda)
- fprintf(f, "CUDA_%u\t", src-nnumas);
- else if (src < nnumas + ncuda + nopencl)
- fprintf(f, "OpenCL%u\t", src-nnumas-ncuda);
- else if (src < nnumas + ncuda + nopencl + nmic)
- fprintf(f, "MIC_%u\t", src-nnumas-ncuda-nopencl);
- else
- fprintf(f, "MPI_MS%u\t", src-nnumas-ncuda-nopencl-nmic);
- for (dst = 0; dst < maxnode; dst++)
- fprintf(f, "%.0f\t", bandwidth_matrix[src][dst]);
- fprintf(f, "\n");
- }
- fprintf(f, "\n");
- for (src = 0; src < maxnode; src++)
- {
- if (src < nnumas)
- fprintf(f, "NUMA_%u\t", src);
- else if (src < nnumas + ncuda)
- fprintf(f, "CUDA_%u\t", src-nnumas);
- else if (src < nnumas + ncuda + nopencl)
- fprintf(f, "OpenCL%u\t", src-nnumas-ncuda);
- else if (src < nnumas + ncuda + nopencl + nmic)
- fprintf(f, "MIC_%u\t", src-nnumas-ncuda-nopencl);
- else
- fprintf(f, "MPI_MS%u\t", src-nnumas-ncuda-nopencl-nmic);
- for (dst = 0; dst < maxnode; dst++)
- fprintf(f, "%.0f\t", latency_matrix[src][dst]);
- fprintf(f, "\n");
- }
- #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)
- if (ncuda != 0 || nopencl != 0)
- fprintf(f, "\nGPU\tNUMA in preference order (logical index), host-to-device, device-to-host\n");
- for (src = 0; src < ncuda + nopencl; src++)
- {
- struct dev_timing *timing;
- struct _starpu_machine_config * config = _starpu_get_machine_config();
- unsigned config_nnumas = _starpu_topology_get_nnumanodes(config);
- unsigned numa;
- #ifdef STARPU_USE_CUDA
- if (src < ncuda)
- {
- fprintf(f, "CUDA_%u\t", src);
- for (numa = 0; numa < config_nnumas; numa++)
- {
- timing = &cudadev_timing_per_numa[src*STARPU_MAXNUMANODES+numa];
- if (timing->timing_htod)
- fprintf(f, "%2d %.0f %.0f\t", timing->numa_id, 1/timing->timing_htod, 1/timing->timing_dtoh);
- else
- fprintf(f, "%2d\t", cuda_affinity_matrix[src][numa]);
- }
- }
- #ifdef STARPU_USE_OPENCL
- else
- #endif
- #endif
- #ifdef STARPU_USE_OPENCL
- {
- fprintf(f, "OpenCL%u\t", src-ncuda);
- for (numa = 0; numa < config_nnumas; numa++)
- {
- timing = &opencldev_timing_per_numa[(src-ncuda)*STARPU_MAXNUMANODES+numa];
- if (timing->timing_htod)
- fprintf(f, "%2d %.0f %.0f\t", timing->numa_id, 1/timing->timing_htod, 1/timing->timing_dtoh);
- else
- fprintf(f, "%2d\t", opencl_affinity_matrix[src][numa]);
- }
- }
- #endif
- fprintf(f, "\n");
- }
- #endif
- }
- static void generate_bus_bandwidth_file(void)
- {
- if (!was_benchmarked)
- benchmark_all_gpu_devices();
- #ifdef STARPU_USE_MPI_MASTER_SLAVE
- /* Slaves don't write files */
- if (!_starpu_mpi_common_is_src_node())
- return;
- #endif
- #ifndef STARPU_SIMGRID
- write_bus_bandwidth_file_content();
- #endif
- }
- static void load_bus_bandwidth_file(void)
- {
- int res;
- char path[256];
- get_bandwidth_path(path, sizeof(path));
- res = access(path, F_OK);
- if (res || !load_bus_bandwidth_file_content())
- {
- /* File does not exist yet or is bogus */
- generate_bus_bandwidth_file();
- }
- }
- #ifndef STARPU_SIMGRID
- /*
- * Config
- */
- static void get_config_path(char *path, size_t maxlen)
- {
- get_bus_path("config", path, maxlen);
- }
- #if defined(STARPU_USE_MPI_MASTER_SLAVE)
- /* check if the master or one slave has to recalibrate */
- static int mpi_check_recalibrate(int my_recalibrate)
- {
- int nb_mpi = _starpu_mpi_src_get_device_count() + 1;
- int mpi_recalibrate[nb_mpi];
- int i;
- MPI_Allgather(&my_recalibrate, 1, MPI_INT, mpi_recalibrate, 1, MPI_INT, MPI_COMM_WORLD);
- for (i = 0; i < nb_mpi; i++)
- {
- if (mpi_recalibrate[i])
- {
- return 1;
- }
- }
- return 0;
- }
- #endif
- static void compare_value_and_recalibrate(char * msg, unsigned val_file, unsigned val_detected)
- {
- int recalibrate = 0;
- if (val_file != val_detected)
- recalibrate = 1;
- #ifdef STARPU_USE_MPI_MASTER_SLAVE
- //Send to each other to know if we had to recalibrate because someone cannot have the correct value in the config file
- recalibrate = mpi_check_recalibrate(recalibrate);
- #endif
- if (recalibrate)
- {
- #ifdef STARPU_USE_MPI_MASTER_SLAVE
- /* Only the master prints the message */
- if (_starpu_mpi_common_is_src_node())
- #endif
- _STARPU_DISP("Current configuration does not match the bus performance model (%s: (stored) %d != (current) %d), recalibrating...\n", msg, val_file, val_detected);
- _starpu_bus_force_sampling();
- #ifdef STARPU_USE_MPI_MASTER_SLAVE
- if (_starpu_mpi_common_is_src_node())
- #endif
- _STARPU_DISP("... done\n");
- }
- }
- static void check_bus_config_file(void)
- {
- int res;
- char path[256];
- struct _starpu_machine_config *config = _starpu_get_machine_config();
- int recalibrate = 0;
- get_config_path(path, sizeof(path));
- res = access(path, F_OK);
- if (res || config->conf.bus_calibrate > 0)
- recalibrate = 1;
- #if defined(STARPU_USE_MPI_MASTER_SLAVE)
- //Send to each other to know if we had to recalibrate because someone cannot have the config file
- recalibrate = mpi_check_recalibrate(recalibrate);
- #endif
- if (recalibrate)
- {
- if (res)
- _STARPU_DISP("No performance model for the bus, calibrating...\n");
- _starpu_bus_force_sampling();
- if (res)
- _STARPU_DISP("... done\n");
- }
- else
- {
- FILE *f;
- int ret;
- unsigned read_cuda = -1, read_opencl = -1, read_mic = -1, read_mpi_ms = -1;
- unsigned read_cpus = -1, read_numa = -1;
- int locked;
- // Loading configuration from file
- f = fopen(path, "r");
- STARPU_ASSERT(f);
- locked = _starpu_frdlock(f) == 0;
- _starpu_drop_comments(f);
- ret = fscanf(f, "%u\t", &read_cpus);
- STARPU_ASSERT(ret == 1);
- _starpu_drop_comments(f);
- ret = fscanf(f, "%u\t", &read_numa);
- STARPU_ASSERT(ret == 1);
- _starpu_drop_comments(f);
- ret = fscanf(f, "%u\t", &read_cuda);
- STARPU_ASSERT(ret == 1);
- _starpu_drop_comments(f);
- ret = fscanf(f, "%u\t", &read_opencl);
- STARPU_ASSERT(ret == 1);
- _starpu_drop_comments(f);
- ret = fscanf(f, "%u\t", &read_mic);
- if (ret == 0)
- read_mic = 0;
- _starpu_drop_comments(f);
- ret = fscanf(f, "%u\t", &read_mpi_ms);
- if (ret == 0)
- read_mpi_ms = 0;
- _starpu_drop_comments(f);
- if (locked)
- _starpu_frdunlock(f);
- fclose(f);
- // Loading current configuration
- ncpus = _starpu_topology_get_nhwcpu(config);
- nnumas = _starpu_topology_get_nnumanodes(config);
- #ifdef STARPU_USE_CUDA
- ncuda = _starpu_get_cuda_device_count();
- #endif
- #ifdef STARPU_USE_OPENCL
- nopencl = _starpu_opencl_get_device_count();
- #endif
- #ifdef STARPU_USE_MIC
- nmic = _starpu_mic_src_get_device_count();
- #endif /* STARPU_USE_MIC */
- #ifdef STARPU_USE_MPI_MASTER_SLAVE
- nmpi_ms = _starpu_mpi_src_get_device_count();
- #endif /* STARPU_USE_MPI_MASTER_SLAVE */
- // Checking if both configurations match
- compare_value_and_recalibrate("CPUS", read_cpus, ncpus);
- compare_value_and_recalibrate("NUMA", read_numa, nnumas);
- compare_value_and_recalibrate("CUDA", read_cuda, ncuda);
- compare_value_and_recalibrate("OpenCL", read_opencl, nopencl);
- compare_value_and_recalibrate("MIC", read_mic, nmic);
- compare_value_and_recalibrate("MPI Master-Slave", read_mpi_ms, nmpi_ms);
- }
- }
- static void write_bus_config_file_content(void)
- {
- FILE *f;
- char path[256];
- int locked;
- STARPU_ASSERT(was_benchmarked);
- get_config_path(path, sizeof(path));
- _STARPU_DEBUG("writing config to %s\n", path);
- f = fopen(path, "w+");
- STARPU_ASSERT(f);
- locked = _starpu_fwrlock(f) == 0;
- _starpu_fftruncate(f, 0);
- fprintf(f, "# Current configuration\n");
- fprintf(f, "%u # Number of CPUs\n", ncpus);
- fprintf(f, "%u # Number of NUMA nodes\n", nnumas);
- fprintf(f, "%u # Number of CUDA devices\n", ncuda);
- fprintf(f, "%u # Number of OpenCL devices\n", nopencl);
- fprintf(f, "%u # Number of MIC devices\n", nmic);
- fprintf(f, "%u # Number of MPI devices\n", nmpi_ms);
- if (locked)
- _starpu_fwrunlock(f);
- fclose(f);
- }
- static void generate_bus_config_file(void)
- {
- if (!was_benchmarked)
- benchmark_all_gpu_devices();
- #ifdef STARPU_USE_MPI_MASTER_SLAVE
- /* Slaves don't write files */
- if (!_starpu_mpi_common_is_src_node())
- return;
- #endif
- write_bus_config_file_content();
- }
- #endif /* !SIMGRID */
- void _starpu_simgrid_get_platform_path(int version, char *path, size_t maxlen)
- {
- if (version == 3)
- get_bus_path("platform.xml", path, maxlen);
- else
- get_bus_path("platform.v4.xml", path, maxlen);
- }
- #ifndef STARPU_SIMGRID
- /*
- * Compute the precise PCI tree bandwidth and link shares
- *
- * We only have measurements from one leaf to another. We assume that the
- * available bandwidth is greater at lower levels, and thus measurements from
- * increasingly far GPUs provide the PCI bridges bandwidths at each level.
- *
- * The bandwidth of a PCI bridge is thus computed as the maximum of the speed
- * of the various transfers that we have achieved through it. We thus browse
- * the PCI tree three times:
- *
- * - first through all CUDA-CUDA possible transfers to compute the maximum
- * measured bandwidth on each PCI link and hub used for that.
- * - then through the whole tree to emit links for each PCI link and hub.
- * - then through all CUDA-CUDA possible transfers again to emit routes.
- */
- #if defined(STARPU_USE_CUDA) && defined(HAVE_DECL_HWLOC_CUDA_GET_DEVICE_OSDEV_BY_INDEX) && HAVE_DECL_HWLOC_CUDA_GET_DEVICE_OSDEV_BY_INDEX && defined(HAVE_CUDA_MEMCPY_PEER)
- /* Records, for each PCI link and hub, the maximum bandwidth seen through it */
- struct pci_userdata
- {
- /* Uplink max measurement */
- double bw_up;
- double bw_down;
- /* Hub max measurement */
- double bw;
- };
- /* Allocate a pci_userdata structure for the given object */
- static void allocate_userdata(hwloc_obj_t obj)
- {
- struct pci_userdata *data;
- if (obj->userdata)
- return;
- _STARPU_MALLOC(obj->userdata, sizeof(*data));
- data = obj->userdata;
- data->bw_up = 0.0;
- data->bw_down = 0.0;
- data->bw = 0.0;
- }
- /* Update the maximum bandwidth seen going to upstream */
- static void update_bandwidth_up(hwloc_obj_t obj, double bandwidth)
- {
- struct pci_userdata *data;
- if (obj->type != HWLOC_OBJ_BRIDGE && obj->type != HWLOC_OBJ_PCI_DEVICE)
- return;
- allocate_userdata(obj);
- data = obj->userdata;
- if (data->bw_up < bandwidth)
- data->bw_up = bandwidth;
- }
- /* Update the maximum bandwidth seen going from upstream */
- static void update_bandwidth_down(hwloc_obj_t obj, double bandwidth)
- {
- struct pci_userdata *data;
- if (obj->type != HWLOC_OBJ_BRIDGE && obj->type != HWLOC_OBJ_PCI_DEVICE)
- return;
- allocate_userdata(obj);
- data = obj->userdata;
- if (data->bw_down < bandwidth)
- data->bw_down = bandwidth;
- }
- /* Update the maximum bandwidth seen going through this Hub */
- static void update_bandwidth_through(hwloc_obj_t obj, double bandwidth)
- {
- struct pci_userdata *data;
- allocate_userdata(obj);
- data = obj->userdata;
- if (data->bw < bandwidth)
- data->bw = bandwidth;
- }
- /* find_* functions perform the first step: computing maximum bandwidths */
- /* Our trafic had to go through the host, go back from target up to the host,
- * updating uplink downstream bandwidth along the way */
- static void find_platform_backward_path(hwloc_obj_t obj, double bandwidth)
- {
- if (!obj)
- /* Oops, we should have seen a host bridge. Well, too bad. */
- return;
- /* Update uplink bandwidth of PCI Hub */
- update_bandwidth_down(obj, bandwidth);
- /* Update internal bandwidth of PCI Hub */
- update_bandwidth_through(obj, bandwidth);
- if (obj->type == HWLOC_OBJ_BRIDGE && obj->attr->bridge.upstream_type == HWLOC_OBJ_BRIDGE_HOST)
- /* Finished */
- return;
- /* Continue up */
- find_platform_backward_path(obj->parent, bandwidth);
- }
- /* Same, but update uplink upstream bandwidth */
- static void find_platform_forward_path(hwloc_obj_t obj, double bandwidth)
- {
- if (!obj)
- /* Oops, we should have seen a host bridge. Well, too bad. */
- return;
- /* Update uplink bandwidth of PCI Hub */
- update_bandwidth_up(obj, bandwidth);
- /* Update internal bandwidth of PCI Hub */
- update_bandwidth_through(obj, bandwidth);
- if (obj->type == HWLOC_OBJ_BRIDGE && obj->attr->bridge.upstream_type == HWLOC_OBJ_BRIDGE_HOST)
- /* Finished */
- return;
- /* Continue up */
- find_platform_forward_path(obj->parent, bandwidth);
- }
- /* Find the path from obj1 through parent down to obj2 (without ever going up),
- * and update the maximum bandwidth along the path */
- static int find_platform_path_down(hwloc_obj_t parent, hwloc_obj_t obj1, hwloc_obj_t obj2, double bandwidth)
- {
- unsigned i;
- /* Base case, path is empty */
- if (parent == obj2)
- return 1;
- /* Try to go down from parent */
- for (i = 0; i < parent->arity; i++)
- if (parent->children[i] != obj1 && find_platform_path_down(parent->children[i], NULL, obj2, bandwidth))
- {
- /* Found it down there, update bandwidth of parent */
- update_bandwidth_down(parent->children[i], bandwidth);
- update_bandwidth_through(parent, bandwidth);
- return 1;
- }
- return 0;
- }
- /* Find the path from obj1 to obj2, and update the maximum bandwidth along the
- * path */
- static int find_platform_path_up(hwloc_obj_t obj1, hwloc_obj_t obj2, double bandwidth)
- {
- int ret;
- hwloc_obj_t parent = obj1->parent;
- if (!parent)
- {
- /* Oops, we should have seen a host bridge. Act as if we had seen it. */
- find_platform_backward_path(obj2, bandwidth);
- return 1;
- }
- if (find_platform_path_down(parent, obj1, obj2, bandwidth))
- /* obj2 was a mere (sub)child of our parent */
- return 1;
- /* obj2 is not a (sub)child of our parent, we have to go up through the parent */
- if (parent->type == HWLOC_OBJ_BRIDGE && parent->attr->bridge.upstream_type == HWLOC_OBJ_BRIDGE_HOST)
- {
- /* We have to go up to the Host, so obj2 is not in the same PCI
- * tree, so we're for for obj1 to Host, and just find the path
- * from obj2 to Host too.
- */
- find_platform_backward_path(obj2, bandwidth);
- update_bandwidth_up(parent, bandwidth);
- update_bandwidth_through(parent, bandwidth);
- return 1;
- }
- /* Not at host yet, just go up */
- ret = find_platform_path_up(parent, obj2, bandwidth);
- update_bandwidth_up(parent, bandwidth);
- update_bandwidth_through(parent, bandwidth);
- return ret;
- }
- /* find the path between cuda i and cuda j, and update the maximum bandwidth along the path */
- static int find_platform_cuda_path(hwloc_topology_t topology, unsigned i, unsigned j, double bandwidth)
- {
- hwloc_obj_t cudai, cudaj;
- cudai = hwloc_cuda_get_device_osdev_by_index(topology, i);
- cudaj = hwloc_cuda_get_device_osdev_by_index(topology, j);
- if (!cudai || !cudaj)
- return 0;
- return find_platform_path_up(cudai, cudaj, bandwidth);
- }
- /* emit_topology_bandwidths performs the second step: emitting link names */
- /* Emit the link name of the object */
- static void emit_pci_hub(FILE *f, hwloc_obj_t obj)
- {
- STARPU_ASSERT(obj->type == HWLOC_OBJ_BRIDGE);
- fprintf(f, "PCI:%04x:[%02x-%02x]", obj->attr->bridge.downstream.pci.domain, obj->attr->bridge.downstream.pci.secondary_bus, obj->attr->bridge.downstream.pci.subordinate_bus);
- }
- static void emit_pci_dev(FILE *f, struct hwloc_pcidev_attr_s *pcidev)
- {
- fprintf(f, "PCI:%04x:%02x:%02x.%1x", pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func);
- }
- /* Emit the links of the object */
- static void emit_topology_bandwidths(FILE *f, hwloc_obj_t obj, const char *Bps, const char *s)
- {
- unsigned i;
- if (obj->userdata)
- {
- struct pci_userdata *data = obj->userdata;
- if (obj->type == HWLOC_OBJ_BRIDGE)
- {
- /* Uplink */
- fprintf(f, " <link id=\"");
- emit_pci_hub(f, obj);
- fprintf(f, " up\" bandwidth=\"%f%s\" latency=\"0.000000%s\"/>\n", data->bw_up, Bps, s);
- fprintf(f, " <link id=\"");
- emit_pci_hub(f, obj);
- fprintf(f, " down\" bandwidth=\"%f%s\" latency=\"0.000000%s\"/>\n", data->bw_down, Bps, s);
- /* PCI Switches are assumed to have infinite internal bandwidth */
- if (!obj->name || !strstr(obj->name, "Switch"))
- {
- /* We assume that PCI Hubs have double bandwidth in
- * order to support full duplex but not more */
- fprintf(f, " <link id=\"");
- emit_pci_hub(f, obj);
- fprintf(f, " through\" bandwidth=\"%f%s\" latency=\"0.000000%s\"/>\n", data->bw * 2, Bps, s);
- }
- }
- else if (obj->type == HWLOC_OBJ_PCI_DEVICE)
- {
- fprintf(f, " <link id=\"");
- emit_pci_dev(f, &obj->attr->pcidev);
- fprintf(f, " up\" bandwidth=\"%f%s\" latency=\"0.000000%s\"/>\n", data->bw_up, Bps, s);
- fprintf(f, " <link id=\"");
- emit_pci_dev(f, &obj->attr->pcidev);
- fprintf(f, " down\" bandwidth=\"%f%s\" latency=\"0.000000%s\"/>\n", data->bw_down, Bps, s);
- }
- }
- for (i = 0; i < obj->arity; i++)
- emit_topology_bandwidths(f, obj->children[i], Bps, s);
- }
- /* emit_pci_link_* functions perform the third step: emitting the routes */
- static void emit_pci_link(FILE *f, hwloc_obj_t obj, const char *suffix)
- {
- if (obj->type == HWLOC_OBJ_BRIDGE)
- {
- fprintf(f, " <link_ctn id=\"");
- emit_pci_hub(f, obj);
- fprintf(f, " %s\"/>\n", suffix);
- }
- else if (obj->type == HWLOC_OBJ_PCI_DEVICE)
- {
- fprintf(f, " <link_ctn id=\"");
- emit_pci_dev(f, &obj->attr->pcidev);
- fprintf(f, " %s\"/>\n", suffix);
- }
- }
- /* Go to upstream */
- static void emit_pci_link_up(FILE *f, hwloc_obj_t obj)
- {
- emit_pci_link(f, obj, "up");
- }
- /* Go from upstream */
- static void emit_pci_link_down(FILE *f, hwloc_obj_t obj)
- {
- emit_pci_link(f, obj, "down");
- }
- /* Go through PCI hub */
- static void emit_pci_link_through(FILE *f, hwloc_obj_t obj)
- {
- /* We don't care about trafic going through PCI switches */
- if (obj->type == HWLOC_OBJ_BRIDGE)
- {
- if (!obj->name || !strstr(obj->name, "Switch"))
- emit_pci_link(f, obj, "through");
- else
- {
- fprintf(f, " <!-- Switch ");
- emit_pci_hub(f, obj);
- fprintf(f, " through -->\n");
- }
- }
- }
- /* Our trafic has to go through the host, go back from target up to the host,
- * using uplink downstream along the way */
- static void emit_platform_backward_path(FILE *f, hwloc_obj_t obj)
- {
- if (!obj)
- /* Oops, we should have seen a host bridge. Well, too bad. */
- return;
- /* Go through PCI Hub */
- emit_pci_link_through(f, obj);
- /* Go through uplink */
- emit_pci_link_down(f, obj);
- if (obj->type == HWLOC_OBJ_BRIDGE && obj->attr->bridge.upstream_type == HWLOC_OBJ_BRIDGE_HOST)
- {
- /* Finished, go through host */
- fprintf(f, " <link_ctn id=\"Host\"/>\n");
- return;
- }
- /* Continue up */
- emit_platform_backward_path(f, obj->parent);
- }
- /* Same, but use upstream link */
- static void emit_platform_forward_path(FILE *f, hwloc_obj_t obj)
- {
- if (!obj)
- /* Oops, we should have seen a host bridge. Well, too bad. */
- return;
- /* Go through PCI Hub */
- emit_pci_link_through(f, obj);
- /* Go through uplink */
- emit_pci_link_up(f, obj);
- if (obj->type == HWLOC_OBJ_BRIDGE && obj->attr->bridge.upstream_type == HWLOC_OBJ_BRIDGE_HOST)
- {
- /* Finished, go through host */
- fprintf(f, " <link_ctn id=\"Host\"/>\n");
- return;
- }
- /* Continue up */
- emit_platform_forward_path(f, obj->parent);
- }
- /* Find the path from obj1 through parent down to obj2 (without ever going up),
- * and use the links along the path */
- static int emit_platform_path_down(FILE *f, hwloc_obj_t parent, hwloc_obj_t obj1, hwloc_obj_t obj2)
- {
- unsigned i;
- /* Base case, path is empty */
- if (parent == obj2)
- return 1;
- /* Try to go down from parent */
- for (i = 0; i < parent->arity; i++)
- if (parent->children[i] != obj1 && emit_platform_path_down(f, parent->children[i], NULL, obj2))
- {
- /* Found it down there, path goes through this hub */
- emit_pci_link_down(f, parent->children[i]);
- emit_pci_link_through(f, parent);
- return 1;
- }
- return 0;
- }
- /* Find the path from obj1 to obj2, and use the links along the path */
- static int emit_platform_path_up(FILE *f, hwloc_obj_t obj1, hwloc_obj_t obj2)
- {
- int ret;
- hwloc_obj_t parent = obj1->parent;
- if (!parent)
- {
- /* Oops, we should have seen a host bridge. Act as if we had seen it. */
- emit_platform_backward_path(f, obj2);
- return 1;
- }
- if (emit_platform_path_down(f, parent, obj1, obj2))
- /* obj2 was a mere (sub)child of our parent */
- return 1;
- /* obj2 is not a (sub)child of our parent, we have to go up through the parent */
- if (parent->type == HWLOC_OBJ_BRIDGE && parent->attr->bridge.upstream_type == HWLOC_OBJ_BRIDGE_HOST)
- {
- /* We have to go up to the Host, so obj2 is not in the same PCI
- * tree, so we're for for obj1 to Host, and just find the path
- * from obj2 to Host too.
- */
- emit_platform_backward_path(f, obj2);
- fprintf(f, " <link_ctn id=\"Host\"/>\n");
- emit_pci_link_up(f, parent);
- emit_pci_link_through(f, parent);
- return 1;
- }
- /* Not at host yet, just go up */
- ret = emit_platform_path_up(f, parent, obj2);
- emit_pci_link_up(f, parent);
- emit_pci_link_through(f, parent);
- return ret;
- }
- /* Clean our mess in the topology before destroying it */
- static void clean_topology(hwloc_obj_t obj)
- {
- unsigned i;
- if (obj->userdata)
- free(obj->userdata);
- for (i = 0; i < obj->arity; i++)
- clean_topology(obj->children[i]);
- }
- #endif
- static void write_bus_platform_file_content(int version)
- {
- FILE *f;
- char path[256];
- unsigned i;
- const char *speed, *flops, *Bps, *s;
- char dash;
- int locked;
- if (version == 3)
- {
- speed = "power";
- flops = "";
- Bps = "";
- s = "";
- dash = '_';
- }
- else
- {
- speed = "speed";
- flops = "f";
- Bps = "Bps";
- s = "s";
- dash = '-';
- }
- STARPU_ASSERT(was_benchmarked);
- _starpu_simgrid_get_platform_path(version, path, sizeof(path));
- _STARPU_DEBUG("writing platform to %s\n", path);
- f = fopen(path, "w+");
- if (!f)
- {
- perror("fopen write_bus_platform_file_content");
- _STARPU_DISP("path '%s'\n", path);
- fflush(stderr);
- STARPU_ABORT();
- }
- locked = _starpu_fwrlock(f) == 0;
- _starpu_fftruncate(f, 0);
- fprintf(f,
- "<?xml version='1.0'?>\n"
- "<!DOCTYPE platform SYSTEM '%s'>\n"
- " <platform version=\"%d\">\n"
- " <config id=\"General\">\n"
- " <prop id=\"network/TCP%cgamma\" value=\"-1\"></prop>\n"
- " <prop id=\"network/latency%cfactor\" value=\"1\"></prop>\n"
- " <prop id=\"network/bandwidth%cfactor\" value=\"1\"></prop>\n"
- " </config>\n"
- " <AS id=\"AS0\" routing=\"Full\">\n"
- " <host id=\"MAIN\" %s=\"1%s\"/>\n",
- version == 3
- ? "http://simgrid.gforge.inria.fr/simgrid.dtd"
- : "http://simgrid.gforge.inria.fr/simgrid/simgrid.dtd",
- version, dash, dash, dash, speed, flops);
- for (i = 0; i < ncpus; i++)
- /* TODO: host memory for out-of-core simulation */
- fprintf(f, " <host id=\"CPU%u\" %s=\"2000000000%s\"/>\n", i, speed, flops);
- for (i = 0; i < ncuda; i++)
- {
- fprintf(f, " <host id=\"CUDA%u\" %s=\"2000000000%s\">\n", i, speed, flops);
- fprintf(f, " <prop id=\"memsize\" value=\"%llu\"/>\n", (unsigned long long) cuda_size[i]);
- #ifdef HAVE_CUDA_MEMCPY_PEER
- fprintf(f, " <prop id=\"memcpy_peer\" value=\"1\"/>\n");
- #endif
- /* TODO: record cudadev_direct instead of assuming it's NUMA nodes */
- fprintf(f, " </host>\n");
- }
- for (i = 0; i < nopencl; i++)
- {
- fprintf(f, " <host id=\"OpenCL%u\" %s=\"2000000000%s\">\n", i, speed, flops);
- fprintf(f, " <prop id=\"memsize\" value=\"%llu\"/>\n", (unsigned long long) opencl_size[i]);
- fprintf(f, " </host>\n");
- }
- fprintf(f, "\n <host id=\"RAM\" %s=\"1%s\"/>\n", speed, flops);
- /*
- * Compute maximum bandwidth, taken as host bandwidth
- */
- double max_bandwidth = 0;
- #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL)
- unsigned numa;
- #endif
- #ifdef STARPU_USE_CUDA
- for (i = 0; i < ncuda; i++)
- {
- for (numa = 0; numa < nnumas; numa++)
- {
- double down_bw = 1.0 / cudadev_timing_per_numa[i*STARPU_MAXNUMANODES+numa].timing_dtoh;
- double up_bw = 1.0 / cudadev_timing_per_numa[i*STARPU_MAXNUMANODES+numa].timing_htod;
- if (max_bandwidth < down_bw)
- max_bandwidth = down_bw;
- if (max_bandwidth < up_bw)
- max_bandwidth = up_bw;
- }
- }
- #endif
- #ifdef STARPU_USE_OPENCL
- for (i = 0; i < nopencl; i++)
- {
- for (numa = 0; numa < nnumas; numa++)
- {
- double down_bw = 1.0 / opencldev_timing_per_numa[i*STARPU_MAXNUMANODES+numa].timing_dtoh;
- double up_bw = 1.0 / opencldev_timing_per_numa[i*STARPU_MAXNUMANODES+numa].timing_htod;
- if (max_bandwidth < down_bw)
- max_bandwidth = down_bw;
- if (max_bandwidth < up_bw)
- max_bandwidth = up_bw;
- }
- }
- #endif
- fprintf(f, "\n <link id=\"Host\" bandwidth=\"%f%s\" latency=\"0.000000%s\"/>\n\n", max_bandwidth*1000000, Bps, s);
- /*
- * OpenCL links
- */
- #ifdef STARPU_USE_OPENCL
- for (i = 0; i < nopencl; i++)
- {
- char i_name[16];
- snprintf(i_name, sizeof(i_name), "OpenCL%u", i);
- fprintf(f, " <link id=\"RAM-%s\" bandwidth=\"%f%s\" latency=\"%f%s\"/>\n",
- i_name,
- 1000000 / search_bus_best_timing(i, "OpenCL", 1), Bps,
- search_bus_best_latency(i, "OpenCL", 1)/1000000., s);
- fprintf(f, " <link id=\"%s-RAM\" bandwidth=\"%f%s\" latency=\"%f%s\"/>\n",
- i_name,
- 1000000 / search_bus_best_timing(i, "OpenCL", 0), Bps,
- search_bus_best_latency(i, "OpenCL", 0)/1000000., s);
- }
- fprintf(f, "\n");
- #endif
- /*
- * CUDA links and routes
- */
- #ifdef STARPU_USE_CUDA
- /* Write RAM/CUDA bandwidths and latencies */
- for (i = 0; i < ncuda; i++)
- {
- char i_name[16];
- snprintf(i_name, sizeof(i_name), "CUDA%u", i);
- fprintf(f, " <link id=\"RAM-%s\" bandwidth=\"%f%s\" latency=\"%f%s\"/>\n",
- i_name,
- 1000000. / search_bus_best_timing(i, "CUDA", 1), Bps,
- search_bus_best_latency(i, "CUDA", 1)/1000000., s);
- fprintf(f, " <link id=\"%s-RAM\" bandwidth=\"%f%s\" latency=\"%f%s\"/>\n",
- i_name,
- 1000000. / search_bus_best_timing(i, "CUDA", 0), Bps,
- search_bus_best_latency(i, "CUDA", 0)/1000000., s);
- }
- fprintf(f, "\n");
- #ifdef HAVE_CUDA_MEMCPY_PEER
- /* Write CUDA/CUDA bandwidths and latencies */
- for (i = 0; i < ncuda; i++)
- {
- unsigned j;
- char i_name[16];
- snprintf(i_name, sizeof(i_name), "CUDA%u", i);
- for (j = 0; j < ncuda; j++)
- {
- char j_name[16];
- if (j == i)
- continue;
- snprintf(j_name, sizeof(j_name), "CUDA%u", j);
- fprintf(f, " <link id=\"%s-%s\" bandwidth=\"%f%s\" latency=\"%f%s\"/>\n",
- i_name, j_name,
- 1000000. / cudadev_timing_dtod[i][j], Bps,
- cudadev_latency_dtod[i][j]/1000000., s);
- }
- }
- #endif
- #if defined(HAVE_DECL_HWLOC_CUDA_GET_DEVICE_OSDEV_BY_INDEX) && HAVE_DECL_HWLOC_CUDA_GET_DEVICE_OSDEV_BY_INDEX && defined(HAVE_CUDA_MEMCPY_PEER)
- /* If we have enough hwloc information, write PCI bandwidths and routes */
- if (!starpu_get_env_number_default("STARPU_PCI_FLAT", 0))
- {
- hwloc_topology_t topology;
- hwloc_topology_init(&topology);
- _starpu_topology_filter(topology);
- hwloc_topology_load(topology);
- /* First find paths and record measured bandwidth along the path */
- for (i = 0; i < ncuda; i++)
- {
- unsigned j;
- for (j = 0; j < ncuda; j++)
- if (i != j)
- if (!find_platform_cuda_path(topology, i, j, 1000000. / cudadev_timing_dtod[i][j]))
- {
- clean_topology(hwloc_get_root_obj(topology));
- hwloc_topology_destroy(topology);
- goto flat_cuda;
- }
- /* Record RAM/CUDA bandwidths */
- find_platform_forward_path(hwloc_cuda_get_device_osdev_by_index(topology, i), 1000000. / search_bus_best_timing(i, "CUDA", 0));
- find_platform_backward_path(hwloc_cuda_get_device_osdev_by_index(topology, i), 1000000. / search_bus_best_timing(i, "CUDA", 1));
- }
- /* Ok, found path in all cases, can emit advanced platform routes */
- fprintf(f, "\n");
- emit_topology_bandwidths(f, hwloc_get_root_obj(topology), Bps, s);
- fprintf(f, "\n");
- for (i = 0; i < ncuda; i++)
- {
- unsigned j;
- for (j = 0; j < ncuda; j++)
- if (i != j)
- {
- fprintf(f, " <route src=\"CUDA%u\" dst=\"CUDA%u\" symmetrical=\"NO\">\n", i, j);
- fprintf(f, " <link_ctn id=\"CUDA%u-CUDA%u\"/>\n", i, j);
- emit_platform_path_up(f,
- hwloc_cuda_get_device_osdev_by_index(topology, i),
- hwloc_cuda_get_device_osdev_by_index(topology, j));
- fprintf(f, " </route>\n");
- }
- fprintf(f, " <route src=\"CUDA%u\" dst=\"RAM\" symmetrical=\"NO\">\n", i);
- fprintf(f, " <link_ctn id=\"CUDA%u-RAM\"/>\n", i);
- emit_platform_forward_path(f, hwloc_cuda_get_device_osdev_by_index(topology, i));
- fprintf(f, " </route>\n");
- fprintf(f, " <route src=\"RAM\" dst=\"CUDA%u\" symmetrical=\"NO\">\n", i);
- fprintf(f, " <link_ctn id=\"RAM-CUDA%u\"/>\n", i);
- emit_platform_backward_path(f, hwloc_cuda_get_device_osdev_by_index(topology, i));
- fprintf(f, " </route>\n");
- }
- clean_topology(hwloc_get_root_obj(topology));
- hwloc_topology_destroy(topology);
- }
- else
- {
- flat_cuda:
- #else
- {
- #endif
- /* If we don't have enough hwloc information, write trivial routes always through host */
- for (i = 0; i < ncuda; i++)
- {
- char i_name[16];
- snprintf(i_name, sizeof(i_name), "CUDA%u", i);
- fprintf(f, " <route src=\"RAM\" dst=\"%s\" symmetrical=\"NO\"><link_ctn id=\"RAM-%s\"/><link_ctn id=\"Host\"/></route>\n", i_name, i_name);
- fprintf(f, " <route src=\"%s\" dst=\"RAM\" symmetrical=\"NO\"><link_ctn id=\"%s-RAM\"/><link_ctn id=\"Host\"/></route>\n", i_name, i_name);
- }
- #ifdef HAVE_CUDA_MEMCPY_PEER
- for (i = 0; i < ncuda; i++)
- {
- unsigned j;
- char i_name[16];
- snprintf(i_name, sizeof(i_name), "CUDA%u", i);
- for (j = 0; j < ncuda; j++)
- {
- char j_name[16];
- if (j == i)
- continue;
- snprintf(j_name, sizeof(j_name), "CUDA%u", j);
- fprintf(f, " <route src=\"%s\" dst=\"%s\" symmetrical=\"NO\"><link_ctn id=\"%s-%s\"/><link_ctn id=\"Host\"/></route>\n", i_name, j_name, i_name, j_name);
- }
- }
- #endif
- } /* defined(STARPU_HAVE_HWLOC) && defined(HAVE_CUDA_MEMCPY_PEER) */
- fprintf(f, "\n");
- #endif /* STARPU_USE_CUDA */
- /*
- * OpenCL routes
- */
- #ifdef STARPU_USE_OPENCL
- for (i = 0; i < nopencl; i++)
- {
- char i_name[16];
- snprintf(i_name, sizeof(i_name), "OpenCL%u", i);
- fprintf(f, " <route src=\"RAM\" dst=\"%s\" symmetrical=\"NO\"><link_ctn id=\"RAM-%s\"/><link_ctn id=\"Host\"/></route>\n", i_name, i_name);
- fprintf(f, " <route src=\"%s\" dst=\"RAM\" symmetrical=\"NO\"><link_ctn id=\"%s-RAM\"/><link_ctn id=\"Host\"/></route>\n", i_name, i_name);
- }
- #endif
- fprintf(f,
- " </AS>\n"
- " </platform>\n"
- );
- if (locked)
- _starpu_fwrunlock(f);
- fclose(f);
- }
- static void generate_bus_platform_file(void)
- {
- if (!was_benchmarked)
- benchmark_all_gpu_devices();
- #ifdef STARPU_USE_MPI_MASTER_SLAVE
- /* Slaves don't write files */
- if (!_starpu_mpi_common_is_src_node())
- return;
- #endif
- write_bus_platform_file_content(3);
- write_bus_platform_file_content(4);
- }
- static void check_bus_platform_file(void)
- {
- int res;
- char path[256];
- _starpu_simgrid_get_platform_path(4, path, sizeof(path));
- res = access(path, F_OK);
- if (!res)
- {
- _starpu_simgrid_get_platform_path(3, path, sizeof(path));
- res = access(path, F_OK);
- }
- if (res)
- {
- /* File does not exist yet */
- generate_bus_platform_file();
- }
- }
- /*
- * Generic
- */
- static void _starpu_bus_force_sampling(void)
- {
- _STARPU_DEBUG("Force bus sampling ...\n");
- _starpu_create_sampling_directory_if_needed();
- generate_bus_affinity_file();
- generate_bus_latency_file();
- generate_bus_bandwidth_file();
- generate_bus_config_file();
- generate_bus_platform_file();
- }
- #endif /* !SIMGRID */
- void _starpu_load_bus_performance_files(void)
- {
- _starpu_create_sampling_directory_if_needed();
- struct _starpu_machine_config * config = _starpu_get_machine_config();
- nnumas = _starpu_topology_get_nnumanodes(config);
- #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_SIMGRID)
- ncuda = _starpu_get_cuda_device_count();
- #endif
- #if defined(STARPU_USE_OPENCL) || defined(STARPU_USE_SIMGRID)
- nopencl = _starpu_opencl_get_device_count();
- #endif
- #if defined(STARPU_USE_MPI_MASTER_SLAVE) || defined(STARPU_USE_SIMGRID)
- nmpi_ms = _starpu_mpi_src_get_device_count();
- #endif
- #if defined(STARPU_USE_MIC) || defined(STARPU_USE_SIMGRID)
- nmic = _starpu_mic_src_get_device_count();
- #endif
- #ifndef STARPU_SIMGRID
- check_bus_config_file();
- #endif
- #ifdef STARPU_USE_MPI_MASTER_SLAVE
- /* be sure that master wrote the perf files */
- _starpu_mpi_common_barrier();
- #endif
- #ifndef STARPU_SIMGRID
- load_bus_affinity_file();
- #endif
- load_bus_latency_file();
- load_bus_bandwidth_file();
- #ifndef STARPU_SIMGRID
- check_bus_platform_file();
- #endif
- }
- /* (in MB/s) */
- double starpu_transfer_bandwidth(unsigned src_node, unsigned dst_node)
- {
- return bandwidth_matrix[src_node][dst_node];
- }
- /* (in µs) */
- double starpu_transfer_latency(unsigned src_node, unsigned dst_node)
- {
- return latency_matrix[src_node][dst_node];
- }
- /* (in µs) */
- double starpu_transfer_predict(unsigned src_node, unsigned dst_node, size_t size)
- {
- double bandwidth = bandwidth_matrix[src_node][dst_node];
- double latency = latency_matrix[src_node][dst_node];
- struct _starpu_machine_topology *topology = &_starpu_get_machine_config()->topology;
- #if 0
- int busid = starpu_bus_get_id(src_node, dst_node);
- int direct = starpu_bus_get_direct(busid);
- #endif
- float ngpus = topology->ncudagpus+topology->nopenclgpus;
- #if 0
- /* Ideally we should take into account that some GPUs are directly
- * connected through a PCI switch, which has less contention that the
- * Host bridge, but doing that seems to *decrease* performance... */
- if (direct)
- {
- float neighbours = starpu_bus_get_ngpus(busid);
- /* Count transfers of these GPUs, and count transfers between
- * other GPUs and these GPUs */
- ngpus = neighbours + (ngpus - neighbours) * neighbours / ngpus;
- }
- #endif
- return latency + (size/bandwidth)*2*ngpus;
- }
- /* calculate save bandwidth and latency */
- /* bandwidth in MB/s - latency in µs */
- void _starpu_save_bandwidth_and_latency_disk(double bandwidth_write, double bandwidth_read, double latency_write, double latency_read, unsigned node)
- {
- unsigned int i, j;
- double slowness_disk_between_main_ram, slowness_main_ram_between_node;
- int print_stats = starpu_get_env_number_default("STARPU_BUS_STATS", 0);
- if (print_stats)
- {
- fprintf(stderr, "\n#---------------------\n");
- fprintf(stderr, "Data transfer speed for %u:\n", node);
- }
- /* save bandwith */
- for(i = 0; i < STARPU_MAXNODES; ++i)
- {
- for(j = 0; j < STARPU_MAXNODES; ++j)
- {
- if (i == j && j == node) /* source == destination == node */
- {
- bandwidth_matrix[i][j] = 0;
- }
- else if (i == node) /* source == disk */
- {
- /* convert in slowness */
- if(bandwidth_read != 0)
- slowness_disk_between_main_ram = 1/bandwidth_read;
- else
- slowness_disk_between_main_ram = 0;
- if(bandwidth_matrix[STARPU_MAIN_RAM][j] != 0)
- slowness_main_ram_between_node = 1/bandwidth_matrix[STARPU_MAIN_RAM][j];
- else
- slowness_main_ram_between_node = 0;
- bandwidth_matrix[i][j] = 1/(slowness_disk_between_main_ram+slowness_main_ram_between_node);
- if (!isnan(bandwidth_matrix[i][j]) && print_stats)
- fprintf(stderr,"%u -> %u: %.0f MB/s\n", i, j, bandwidth_matrix[i][j]);
- }
- else if (j == node) /* destination == disk */
- {
- /* convert in slowness */
- if(bandwidth_write != 0)
- slowness_disk_between_main_ram = 1/bandwidth_write;
- else
- slowness_disk_between_main_ram = 0;
- if(bandwidth_matrix[i][STARPU_MAIN_RAM] != 0)
- slowness_main_ram_between_node = 1/bandwidth_matrix[i][STARPU_MAIN_RAM];
- else
- slowness_main_ram_between_node = 0;
- bandwidth_matrix[i][j] = 1/(slowness_disk_between_main_ram+slowness_main_ram_between_node);
- if (!isnan(bandwidth_matrix[i][j]) && print_stats)
- fprintf(stderr,"%u -> %u: %.0f MB/s\n", i, j, bandwidth_matrix[i][j]);
- }
- else if (j > node || i > node) /* not affected by the node */
- {
- bandwidth_matrix[i][j] = NAN;
- }
- }
- }
- /* save latency */
- for(i = 0; i < STARPU_MAXNODES; ++i)
- {
- for(j = 0; j < STARPU_MAXNODES; ++j)
- {
- if (i == j && j == node) /* source == destination == node */
- {
- latency_matrix[i][j] = 0;
- }
- else if (i == node) /* source == disk */
- {
- latency_matrix[i][j] = (latency_write+latency_matrix[STARPU_MAIN_RAM][j]);
- if (!isnan(latency_matrix[i][j]) && print_stats)
- fprintf(stderr,"%u -> %u: %.0f µs\n", i, j, latency_matrix[i][j]);
- }
- else if (j == node) /* destination == disk */
- {
- latency_matrix[i][j] = (latency_read+latency_matrix[i][STARPU_MAIN_RAM]);
- if (!isnan(latency_matrix[i][j]) && print_stats)
- fprintf(stderr,"%u -> %u: %.0f µs\n", i, j, latency_matrix[i][j]);
- }
- else if (j > node || i > node) /* not affected by the node */
- {
- latency_matrix[i][j] = NAN;
- }
- }
- }
- if (print_stats)
- fprintf(stderr, "\n#---------------------\n");
- }
|