@@ -456,7 +456,9 @@ void HsaQueueErrorCallback(hsa_status_t status, hsa_queue_t *source, void *data)
456456
457457 status = Runtime ()->Hsa ()->hsa_ext_image_destroy (Runtime ()->Agent (), image);
458458 if (status != HSA_STATUS_SUCCESS) { Runtime ()->HsaError (" hsa_ext_image_destroy failed" , status); }
459- alignedFree (data);
459+ // alignedFree(data);
460+ status = Runtime ()->Hsa ()->hsa_memory_free (data);
461+ if (status != HSA_STATUS_SUCCESS) { Runtime ()->HsaError (" hsa_memory_free failed" , status); }
460462 }
461463
462464 virtual bool ImageInitialize (const std::string& imageId, const std::string& imageParamsId,
@@ -519,6 +521,28 @@ void HsaQueueErrorCallback(hsa_status_t status, hsa_queue_t *source, void *data)
519521 {
520522 hsa_status_t status;
521523
524+ class ImageRegionMatcher {
525+ private:
526+ hsa_ext_image_data_info_t image_info;
527+
528+ public:
529+ ImageRegionMatcher (hsa_ext_image_data_info_t image_info_): image_info(image_info_) {};
530+
531+ bool operator () (HsailRuntimeContext* runtime, hsa_region_t region) {
532+ size_t align = 0 ;
533+ hsa_region_segment_t seg;
534+
535+ runtime->Hsa ()->hsa_region_get_info (region, HSA_REGION_INFO_SEGMENT, &seg);
536+ if (seg == HSA_REGION_SEGMENT_GLOBAL)
537+ {
538+ runtime->Hsa ()->hsa_region_get_info (region, HSA_REGION_INFO_RUNTIME_ALLOC_ALIGNMENT, &align);
539+ if (align >= image_info.alignment )
540+ return true ;
541+ }
542+ return false ;
543+ }
544+ };
545+
522546 const ImageParams* ip = context->Get <ImageParams>(imageParamsId);
523547
524548 hsa_access_permission_t access_permission = ImageType2HsaAccessPermission (ip->imageType );
@@ -564,28 +588,29 @@ void HsaQueueErrorCallback(hsa_status_t status, hsa_queue_t *source, void *data)
564588
565589 hsa_ext_image_t image = {0 };
566590 void *imageData = NULL ;
567- size_t size = (std::max)(image_info.size , (size_t ) 256 );
568- imageData = alignedMalloc (size, image_info.alignment );
591+ // size_t size = (std::max)(image_info.size, (size_t) 256);
592+ // imageData = alignedMalloc(size, image_info.alignment);
569593
570594 /*
571595 status = Runtime()->Hsa()->hsa_memory_register(imageData, size);
572596 if (status != HSA_STATUS_SUCCESS) { Runtime()->HsaError("hsa_memory_register (image data) failed", status); alignedFree(imageData); return 0; }
573597 */
574598
575- /*
599+ // *
576600 hsa_region_t region = Runtime ()->GetRegion (ImageRegionMatcher (image_info));
577601 if (!region.handle ) { Runtime ()->HsaError (" Failed to find image region" ); return 0 ; }
578- status = Runtime()->Hsa()->hsa_memory_allocate(region, image_info.size, &ptr );
602+ status = Runtime ()->Hsa ()->hsa_memory_allocate (region, image_info.size , &imageData );
579603 if (status != HSA_STATUS_SUCCESS) { Runtime ()->HsaError (" hsa_memory_allocate failed" , status); return 0 ; }
580- */
604+ // */
581605
582606 status = Runtime ()->Hsa ()->hsa_ext_image_create (Runtime ()->Agent (), &image_descriptor, imageData, access_permission, &image);
583607 if (status == HSA_STATUS_ERROR_OUT_OF_RESOURCES) {
584608 context->Move (TEST_STATUS_KEY, new TestStatus (NA));
585609 return false ;
586610 }
587611 if (status != HSA_STATUS_SUCCESS) {
588- Runtime ()->HsaError (" hsa_ext_image_create failed" , status); alignedFree (imageData);
612+ Runtime ()->HsaError (" hsa_ext_image_create failed" , status);
613+ Runtime ()->Hsa ()->hsa_memory_free (imageData);
589614 return false ;
590615 }
591616
0 commit comments