[代码]
/************************************************************************* * Copyright (C) [2018] by Cambricon, Inc. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *************************************************************************/ // TODO:PowerDifference BCL多核实现 #define BASE_NUM 64 #define CORE_NUM 16 __mlu_entry__ void PowerDifferenceKernel(half* in1, half* in2, int pow, half* out, int len) { // if (taskId > 0) return; // TODO:循环条件判断 len = len/CORE_NUM; in1 = in1+taskId*len; in2 = in2+taskId*len; out = out+taskId*len; if (taskId == CORE_NUM-1) len+=len%CORE_NUM; // printf("%d %d", len, taskId); int quotient = len/BASE_NUM; int remainder = len%BASE_NUM; // TODO:内存申请 __nram__ half tmp1[BASE_NUM]; __nram__ half tmp2[BASE_NUM]; __nram__ half tmp_pow[BASE_NUM]; // TODO:For循环计算 for (int i = 0; i < quotient; i++){ // TODO:拷入操作 __memcpy(tmp1, (in1+i*BASE_NUM), (BASE_NUM*sizeof(half)), GDRAM2NRAM); __memcpy(tmp2, (in2+i*BASE_NUM), (BASE_NUM*sizeof(half)), GDRAM2NRAM); // TODO:实际计算部分 __bang_sub(tmp1, tmp1, tmp2, len); // 快速幂 // if (pow==1){ // __memcpy(tmp_pow, tmp1, (BASE_NUM*sizeof(half)), NRAM2NRAM); // } // else if (pow==2){ // __bang_mul(tmp_pow, tmp1, tmp1, len); // }else{ // bool r=true; // while(pow!=0){ // if(pow%2){ // if(r){ // __memcpy(tmp_pow, tmp1, (BASE_NUM*sizeof(half)), NRAM2NRAM); // r = false; // }else{ // __bang_mul(tmp2, tmp1, tmp_pow, len); // __memcpy(tmp_pow, tmp2, (BASE_NUM*sizeof(half)), NRAM2NRAM); // __bang_printf("a%#x ", tmp_pow[0]); // __bang_printf("b%#x ", tmp1[0]); // } // } // if (pow!=1){ // __bang_mul(tmp1, tmp1, tmp1, len); // __bang_printf("c%#x ", tmp_pow[0]); // } // pow/=2; // } // } __bang_mul(tmp_pow, tmp1, tmp1, len); __bang_printf("a%#x ", tmp_pow[0]); __bang_mul(tmp1, tmp_pow, tmp1, len); __bang_printf("b%#x ", tmp_pow[0]); // __memcpy(tmp_pow, tmp1, (BASE_NUM*sizeof(half)), NRAM2NRAM); // for (int j = 0; j < pow-1; j++){ // __bang_mul(tmp_pow, tmp_pow, tmp1, len); // } // TODO:结果拷出操作 __memcpy((out+i*BASE_NUM), tmp_pow, (BASE_NUM*sizeof(half)), NRAM2GDRAM); } if (remainder != 0){ // TODO:拷入操作 __memcpy(tmp1, (in1+quotient*BASE_NUM), (remainder*sizeof(half)), GDRAM2NRAM); __memcpy(tmp2, (in2+quotient*BASE_NUM), (remainder*sizeof(half)), GDRAM2NRAM); // TODO:实际计算部分 __bang_sub(tmp1, tmp1, tmp2, len); __memcpy(tmp_pow, tmp1, (BASE_NUM*sizeof(half)), NRAM2NRAM); for (int j = 0; j < pow-1; j++){ __bang_mul(tmp_pow, tmp_pow, tmp1, len); } // TODO:结果拷出操作 __memcpy((out+quotient*BASE_NUM), tmp1, (remainder*sizeof(half)), NRAM2GDRAM); } }
请登录后评论