// 縦、横、斜め、それぞれの合計値を求め、MARU/BATUが揃っているかを計算で求める int check_finish(int depth) { // Nより小さいときはチェックする必要なし if (depth <= N) { return 0; } { // 縦と横 for (int i=0; i<N; i++) { int row=0, col=0; for (int j=0; j<N; j++) { row += cells[i*N + j]; col += cells[i + j*N]; } if (check_sum(row)) return 1; if (check_sum(col)) return 1; } } // Nが奇数の時だけ斜め処理する if (N % 2) { { // 斜め(右下がり、右上がり) int shita=0, ue=0; for (int index=0; index<N; index++) { shita += cells[index*N + index]; ue += cells[index*N + (N-index-1)]; } if (check_sum(shita)) return 1; if (check_sum(ue)) return 1; } } return 0; }
STATIC_PREFIX int fw_load_extl(unsigned por_cfg,unsigned target,unsigned size) { unsigned temp_addr; #ifdef CONFIG_MESON_TRUSTZONE unsigned secure_addr; unsigned secure_size; unsigned *sram; #endif #if CONFIG_UCL temp_addr=target-0x800000; #else temp_addr=target; #endif int rc=sdio_read(temp_addr,size,por_cfg); #if defined(CONFIG_M6_SECU_BOOT) aml_m6_sec_boot_check((const unsigned char *)temp_addr); #endif //CONFIG_M6_SECU_BOOT #ifdef CONFIG_MESON_TRUSTZONE sram = (unsigned*)(AHB_SRAM_BASE + READ_SIZE-SECURE_OS_OFFSET_POSITION_IN_SRAM); secure_addr = (*sram) + temp_addr - READ_SIZE; sram = (unsigned*)(AHB_SRAM_BASE + READ_SIZE-SECURE_OS_SIZE_POSITION_IN_SRAM); secure_size = (*sram); secure_load(secure_addr, secure_size); #endif #if CONFIG_UCL #ifndef CONFIG_IMPROVE_UCL_DEC unsigned len; if(!rc){ serial_puts("ucl decompress\n"); rc=uclDecompress((char*)target,&len,(char*)temp_addr); serial_puts("decompress finished\n"); } #endif #endif #ifndef CONFIG_IMPROVE_UCL_DEC if(!rc) rc=check_sum((unsigned*)target,magic_info->crc[1],size); #else if(!rc) rc=check_sum((unsigned*)temp_addr,magic_info->crc[1],size); #endif return rc; }
static int search_cmd_process(unsigned char *buf, int cmd_len, const struct sockaddr_t *dest_addr, socklen_t addrlen) { u32 len=0; mxchip_cmd_head_t *p_reply; udp_search_st *data; u16 cksum, cmd; if(device_info->udpSearch_fd == 0) return 0; if ((buf[0] != CONTROL_FLAG) || (buf[1] != 0)) return 0; if (check_sum(hugebuf, cmd_len) != 1) return 0; p_reply = (mxchip_cmd_head_t *)buf; //Use the same data block to save the RAM data = (udp_search_st *)(p_reply->data); p_reply->cmd_status = CMD_OK; cmd = p_reply->cmd; p_reply->cmd |= 0x8000; if(cmd == CMD_SEARCH){ p_reply->datalen = sizeof(udp_search_st); memcpy(data->ip, device_info->status.ip, 16); memcpy(data->mac, device_info->status.mac, 18); sprintf((char *)data->mac, "%08x", FW_VERSION); len = sizeof(mxchip_cmd_head_t) + 1 + p_reply->datalen; cksum = calc_sum(hugebuf, len-2); hugebuf[len-2] = cksum & 0x0ff; hugebuf[len-1] = (cksum & 0xff00) >> 8; sendto(device_info->udpSearch_fd, (u8*)p_reply, sizeof(mxchip_cmd_head_t) + 1 + p_reply->datalen, \ 0, dest_addr, addrlen); }
/** ****************************************************************************** * @brief receive_packet * @param[in] None * @param[out] uint8 *cmd : 命令字 * @param[out] uint8 *data : 接收缓冲 * @param[out] uint8 *size : data长度 * @retval true-成功、 false-失败 * * @details 接收一个数据包 * * @note ****************************************************************************** */ boolean receive_packet(uint8 *cmd, uint8 *data, uint8 *size) { uint8 sum; // 读取帧长度字节 do { uart_receive(size, 1); } while (*size < 3); // 帧长度 >= 3 // 读取帧校验字节 uart_receive(&sum, 1); // 读取命令字 uart_receive(cmd, 1); // 扣除长度、命令字及校验字节,还需读取的字节数 *size -= 3; if (*size > 0) uart_receive(data, *size); // 如果帧校验错误,返回false if (!check_sum(*cmd, data, *size, sum)) return (false); return (true); }
bool check_square(int **matr, int n)//главная функция проверки { if ((check_sum(matr, n) == 1) && (check_degr(matr, n) == 1) && (check_num(matr, n) == 1))//если все функции возращют 1 return true;//возвращаем true else return false;//иначе возвращаем false }
int upload_program( unsigned int start, unsigned int length ) { static unsigned char upload_program_string[] = "$UP "; int count; unsigned long check; if (length * 3 > sizeof(buffer)) return 0; upload_program_string[3] = start >> 8; upload_program_string[4] = start; upload_program_string[5] = length >> 8; upload_program_string[6] = length; write_drain( ttyfd, upload_program_string, sizeof(upload_program_string) - 1 ); /* bytes received not counting check sum */ count = received( 3 + 3 * length ) - 3; if ( count > 0 ) { check = check_sum( buffer, count, 3 ); if (check != program_word( buffer + count )) { fprintf( stderr, "Bad data check sum: %x/%x\n", program_word( buffer + count ), check ); count = 0; } } else { count = 0; } return count; }
// // NOT: x - lower case // bool make_read_command( COMMR572_CMND_ID cmnd_id, int parameter, char *cmnd_buffer ) { char cmnd_param[16]; int i; short cs; for (i = 0; i < N_OF_COMMR572_CMND_IDS; i++ ) { if (commr572_cmnd[i].cmnd_id == cmnd_id) { // add 4 hex digits as a command parameter sprintf(cmnd_param, "%02x%04x", commr572_cmnd[i].read_command, parameter ); break; } } if ( i >= N_OF_COMMR572_CMND_IDS ) { return FALSE; } cs = check_sum( cmnd_param ); sprintf(cmnd_buffer, "%c%s%02x%c", STX, cmnd_param, cs, ETX); return TRUE; }
/******************************************************************************** *函数名称: check_file *函数原型: __s32 check_file( __u32 *mem_base, __u32 size, const char *magic ) *函数功能: 使用“算术和”来校验内存中的一段数据 *入口参数: mem_base 待校验的数据在内存中的起始地址(必须是4字节对齐的) * size 待校验的数据的个数(以字节为单位,必须是4字节对齐的) * magic magic number, 待校验文件的标识码 *返 回 值: CHECK_IS_CORRECT 校验正确 * CHECK_IS_WRONG 校验错误 *备 注: ********************************************************************************/ __s32 check_file( __u32 *mem_base, __u32 size, const char *magic ) { if( check_magic( mem_base, magic ) == 0 &&check_sum( mem_base, size ) == 0 ) return 0; else return -1; }
/******************************************************************************** *函数名称: check_file *函数原型: __s32 check_file( __u32 *mem_base, __u32 size, const char *magic ) *函数功能: 使用“算术和”来校验内存中的一段数据 *入口参数: mem_base 待校验的数据在内存中的起始地址(必须是4字节对齐的) * size 待校验的数据的个数(以字节为单位,必须是4字节对齐的) * magic magic number, 待校验文件的标识码 *返 回 值: CHECK_IS_CORRECT 校验正确 * CHECK_IS_WRONG 校验错误 *备 注: ********************************************************************************/ __s32 check_file( __u32 *mem_base, __u32 size, const char *magic ) { if( check_magic( mem_base, magic ) == CHECK_IS_CORRECT &&check_sum( mem_base, size ) == CHECK_IS_CORRECT ) return CHECK_IS_CORRECT; else return CHECK_IS_WRONG; }
//----------------------------------------------------- //处理接收到的数据 //return:1-收到命令正确;0-收到命令错误 void rece_dispatch() { uint8_t len=buffer[0]; uint8_t error=0;; if(buffer[1]!=0xfe || buffer[2]!=0x04 || (buffer[len]!=check_sum(&buffer[1],len-1)) ) { //收到错误命令回复 send_shakehand(0); } //解析命令 switch(buffer[3]) { case 0x01://确认命令 break; case 0x02://初始化flash存储区 flash_init(); break; case 0x03://收到时间设置命令 RTC_Set_datetime(&buffer[5]); RTC_AlarmConfig(buffer[8],buffer[9]+1); //SEGGER_RTT_printf(0,"set_time=%02d-%02d-%02d;%02d-%02d-%02d;\r\n",buffer[5],buffer[6],buffer[7],buffer[8],buffer[9],buffer[10]); break; case 0x04://step length sys_para.step_len=buffer[5]; sys_para.height=*(uint16_t *)(&buffer[6]); //flash_erase_para_sector(); break; case 0x05://return serial number send_version_info(); break; case 0x0a://模式切换 if(buffer[5]>0x03) error=1; else curr_mode=buffer[5]; break; case 0x06://read now sports data send_sensor_data(); break; case 0x09://中断批量传输 bat_upload=0; break; default: send_shakehand(0);//收到异常命令S return; }; SEGGER_RTT_printf(0,"rece comm=%x;\r\n",buffer[3]); if(buffer[3]!=0x01) { //回复确认收到 if(error==0) send_shakehand(1); else send_shakehand(0); } }
static int lme2510_download_firmware(struct usb_device *dev, const struct firmware *fw) { int ret = 0; u8 data[512] = {0}; u16 j, wlen, len_in, start, end; u8 packet_size, dlen, i; u8 *fw_data; packet_size = 0x31; len_in = 1; info("FRM Starting Firmware Download"); for (i = 1; i < 3; i++) { start = (i == 1) ? 0 : 512; end = (i == 1) ? 512 : fw->size; for (j = start; j < end; j += (packet_size+1)) { fw_data = (u8 *)(fw->data + j); if ((end - j) > packet_size) { data[0] = i; dlen = packet_size; } else { data[0] = i | 0x80; dlen = (u8)(end - j)-1; } data[1] = dlen; memcpy(&data[2], fw_data, dlen+1); wlen = (u8) dlen + 4; data[wlen-1] = check_sum(fw_data, dlen+1); deb_info(1, "Data S=%02x:E=%02x CS= %02x", data[3], data[dlen+2], data[dlen+3]); ret |= lme2510_bulk_write(dev, data, wlen, 1); ret |= lme2510_bulk_read(dev, data, len_in , 1); ret |= (data[0] == 0x88) ? 0 : -1; } } usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), 0x06, 0x80, 0x0200, 0x00, data, 0x0109, 1000); data[0] = 0x8a; len_in = 1; msleep(2000); ret |= lme2510_bulk_write(dev, data , len_in, 1); /*Resetting*/ ret |= lme2510_bulk_read(dev, data, len_in, 1); msleep(400); if (ret < 0) info("FRM Firmware Download Failed (%04x)" , ret); else info("FRM Firmware Download Completed - Resetting Device"); return (ret < 0) ? -ENODEV : 0; }
static int lme2510_download_firmware(struct dvb_usb_device *d, const struct firmware *fw) { int ret = 0; u8 *data; u16 j, wlen, len_in, start, end; u8 packet_size, dlen, i; u8 *fw_data; packet_size = 0x31; len_in = 1; data = kzalloc(128, GFP_KERNEL); if (!data) { info("FRM Could not start Firmware Download"\ "(Buffer allocation failed)"); return -ENOMEM; } info("FRM Starting Firmware Download"); for (i = 1; i < 3; i++) { start = (i == 1) ? 0 : 512; end = (i == 1) ? 512 : fw->size; for (j = start; j < end; j += (packet_size+1)) { fw_data = (u8 *)(fw->data + j); if ((end - j) > packet_size) { data[0] = i; dlen = packet_size; } else { data[0] = i | 0x80; dlen = (u8)(end - j)-1; } data[1] = dlen; memcpy(&data[2], fw_data, dlen+1); wlen = (u8) dlen + 4; data[wlen-1] = check_sum(fw_data, dlen+1); deb_info(1, "Data S=%02x:E=%02x CS= %02x", data[3], data[dlen+2], data[dlen+3]); lme2510_usb_talk(d, data, wlen, data, len_in); ret |= (data[0] == 0x88) ? 0 : -1; } } data[0] = 0x8a; len_in = 1; msleep(2000); lme2510_usb_talk(d, data, len_in, data, len_in); msleep(400); if (ret < 0) info("FRM Firmware Download Failed (%04x)" , ret); else info("FRM Firmware Download Completed - Resetting Device"); kfree(data); return RECONNECTS_USB; }
//=============================================================== //-------------------------------------- //解析长度命令 uint16_t get_len(uint8_t *data) { if(data[2]==0xaa && (data[5]==check_sum(data,5)) ) { return data[4]; } else return 0xff; }
double __card_write_remain(struct user_card * c, double money) { unsigned int imoney = money * 100; c->card.sector_4.data.remain_money[0] = imoney & 0xFF; c->card.sector_4.data.remain_money[1] = (imoney >> 8) & 0xFF; c->card.sector_4.data.remain_money[2] = (imoney >> 16) & 0xFF; c->card.sector_4.data.remain_sum = check_sum(c->card.sector_4.data.remain_money, 3); return money; }
//--------------------------------------------------- //模式切换上报 void send_mode() { buffer[0]=0xfe; buffer[1]=0x05; buffer[2]=0x08;//command buffer[3]=0x0a; buffer[4]=curr_mode; buffer[5]=check_sum(buffer,5); uart2_send(buffer,6); }
void test_accumulate(Iterator first, Iterator last, T zero, const char *label) { int i; start_timer(); for(i = 0; i < iterations; ++i) check_sum( double( accumulate(first, last, zero) ) ); record_result( timer(), label ); }
void MainWindow::on_pushButton_clicked() { count = count + check_sum(); if (count >= 99) { count = count - 99; } this->ui->lcdNumber->display(count); volume_changed(count); std::cout << "for them! " <<"\n"; }
STATIC_PREFIX int fw_load_extl(unsigned por_cfg,unsigned target,unsigned size) { unsigned temp_addr; #if CONFIG_UCL temp_addr=target-0x800000; #else temp_addr=target; #endif int rc=sdio_read(temp_addr,size,por_cfg); #if defined(CONFIG_AML_SECU_BOOT_V2) if(aml_sec_boot_check((unsigned char *)temp_addr)) { AML_WATCH_DOG_START(); } #endif //CONFIG_AML_SECU_BOOT_V2 #if CONFIG_UCL #ifndef CONFIG_IMPROVE_UCL_DEC unsigned len; if(!rc){ serial_puts("ucl decompress..."); rc=uclDecompress((char*)target,&len,(char*)temp_addr); //serial_puts("decompress finished\n"); serial_puts(rc?"fail\n":"pass\n"); } #endif #endif #ifndef CONFIG_IMPROVE_UCL_DEC if(!rc) rc=check_sum((unsigned*)target,magic_info->crc[1],size); #else if(!rc) rc=check_sum((unsigned*)temp_addr,magic_info->crc[1],size); #endif return rc; }
/*function:实现数据的接收和数据的初步解析 input: recv_packet接收数据放在这个包里 recv_total_len可以接收的最大字节数 timeout 超时 output: 当前串口数据状态,SERIAL_STATUS_OK 成功接收一帧数据 */ static int recv_pack(char *recv_packet, int recv_total_len, int *recv_len,int timeout) { // int recv_len=0;//recv_packet中接收的总字节数 int pack_len=0;//接收包的总长度 *recv_len = 0; while(1) { int ret,i; int read_len; ret = serial_poll(serial_id,timeout); if (ret < 0) return SERIAL_STATUS_PORT_ERR; if (ret == 0) return SERIAL_STATUS_TIMEOUT; read_len = serial_read(serial_id, recv_packet + *recv_len, recv_total_len-(*recv_len)); if (read_len <= 0)//未收到数据 return SERIAL_STATUS_PORT_ERR; *recv_len += read_len;//数据长度 // return SERIAL_STATUS_OK; /*数据初步解析*/ if(recv_packet[0] != 0xa5 || recv_packet[1] != 0xa5)//包头不对,放弃该数据 { //serial_flush(serial_id, SERIAL_FLUSH_RX);//清空接收缓存 return SERIAL_STATUS_PACK_ERR; } pack_len = recv_packet[2]*256 + recv_packet[3];//读取指令长度 pack_len = pack_len+6;//根据协议,添加上头尾的长度 if(pack_len <= *recv_len)//一帧数据接收完毕 { if((recv_packet[pack_len-1] != 0x5a) || (recv_packet[pack_len-2] != 0x5a) || !check_sum(recv_packet , pack_len))//包尾校验,校验和 { //serial_flush(serial_id, SERIAL_FLUSH_RX);//清空接收缓存 return SERIAL_STATUS_PACK_ERR; } //serial_flush(serial_id, SERIAL_FLUSH_RX);//清空接收缓存 return SERIAL_STATUS_OK; } if(pack_len < *recv_len) { // serial_flush(serial_id, SERIAL_FLUSH_RX);//清空接收缓存 // return SERIAL_STATUS_PACK_ERR; } } }
//================================================================ //返回版本信息, void send_version_info() { uint8_t i=0; buffer[0]=0xfe; buffer[1]=0x05; buffer[2]=0x05;//command buffer[3]=0x0e; for(;i<12;i++) buffer[4+i]=serian_no[i]; buffer[4+i]=main_ver; buffer[5+i]=sec_ver; buffer[6+i]=check_sum(buffer,6+i); uart2_send(buffer,7+i); }
bool choose_partition(size_t row = 0) { while (row < _choice.size()) { if (_choice[row]) { _choice[row] = -1; if (choose_partition(row + 1)) return true; _choice[row] = 1; if (choose_partition(row + 1)) return true; return false; } else ++row; } return check_sum(); }
//--------------------------------------------------- //传输传感器数据 void send_sensor_data() { uint8_t *temp=NULL; uint8_t i=0; buffer[0]=0xfe; buffer[1]=0x05; buffer[2]=0x06;//command buffer[3]=0x05; if(current_mode==3) temp=(uint8_t *)&(current_sensor_data.sleep_status); else temp=(uint8_t *)&(current_sensor_data.step_count); memcpy(&buffer[4],temp,3); buffer[7]=current_sensor_data.hrs_rate; buffer[8]=batt_status; buffer[9]=check_sum(buffer,9); uart2_send(buffer,10); }
void evolve_pop_details(gsl_rng * rng, singlepop_t * pop, const std::vector<unsigned> & nlist, const double & theta, const double & rho) { double mu = theta/(4.*double(pop->N)),littler=rho/(4.*double(pop->N)); std::function<double(void)> recmap = std::bind(gsl_rng_uniform,rng); for( unsigned generation = 0; generation < nlist.size() ; ++generation,++pop->generation ) { //Iterate the population through 1 generation double wbar = sample_diploid(rng, &pop->gametes, //non-const pointer to gametes &pop->diploids, //non-const pointer to diploids &pop->mutations, //non-const pointer to mutations pop->N, //current pop size nlist[generation], //next pop size, mu, //mutation rate per gamete std::bind(infsites(),rng,&pop->mut_lookup,generation, mu,0.,[&rng](){return gsl_rng_uniform(rng);},[](){return 0.;},[](){return 0.;}), //The recombination policy includes the uniform crossover rate std::bind(genetics101(),std::placeholders::_1,std::placeholders::_2, std::placeholders::_3, //Pass as reference std::ref(pop->neutral),std::ref(pop->selected), &pop->gametes, littler, rng, recmap), std::bind(insert_at_end<singlepop_t::mutation_t,singlepop_t::mlist_t>,std::placeholders::_1,std::placeholders::_2), std::bind(insert_at_end<singlepop_t::gamete_t,singlepop_t::glist_t>,std::placeholders::_1,std::placeholders::_2), std::bind(multiplicative_diploid(),std::placeholders::_1,2.), std::bind(mutation_remover(),std::placeholders::_1,0,2*nlist[generation])); //update pop size in data structure pop->N = nlist[generation]; remove_fixed_lost(&pop->mutations,&pop->fixations,&pop->fixation_times,&pop->mut_lookup,generation,2*pop->N); assert(check_sum(pop->gametes,2*pop->N)); } }
// Make sure that the barcode supplied can be verified using the locations.txt file void correct_location(void) { validated_locations = fopen("validated_locations.txt", "r"); validated_barcodes = fopen("validated_barcodes.txt", "r"); fpos_t start_position; fgetpos(validated_locations, &start_position); char string[1000]; char buffer[2000]; int barcode[12]; int buffer_int[2000]; while (fgets(string, sizeof(string), validated_barcodes)) { fsetpos(validated_locations, &start_position); int found = 0; while (fgets(buffer, sizeof(buffer), validated_locations)) { int i; int indicator = 0; for (i = 0; i < 4; i++) { barcode[i] = string[i] - '0'; buffer_int[i] = buffer[i] - '0'; if (barcode[i] != buffer_int[i]) { indicator = 1; break; } } if (indicator == 1) { continue; } found = 1; check_sum(string); break; } if (found == 0) { bad_location(string); } } fclose(validated_locations); fclose(validated_barcodes); }
void ats_send_cmd(u8* buf,u8 len) { u8 cmd[32]; u8 index; u8 i; index= 0; cmd[index++] = 'S'; cmd[index++] = 'L'; cmd[index++] = 'B'; cmd[index++] = len+1;//cm+arg0+arg1...+checksume for(i=0;i<len;i++) { cmd[index++]=buf[i]; } cmd[index++]=check_sum(cmd+3,index-3); uart0_send(cmd,index); }
/* Packet format: BB 00 CMD(2B) Status(2B) datalen(2B) data(x) checksum(2B) * copy to buf, return len = datalen+10 */ int _uart_get_one_packet(uint8_t* inBuf, int inBufLen) { uart_recv_log_trace(); OSStatus err = kNoErr; int datalen; uint8_t *p; while(1) { p = inBuf; err = PlatformUartRecv(p, 1, MICO_WAIT_FOREVER); require_noerr(err, exit); require(*p == 0xBB, exit); p++; err = PlatformUartRecv(p, 1, 1000); require_noerr(err, exit); require(*p == 0x00, exit); p++; err = PlatformUartRecv(p, 6, 1000); require_noerr(err, exit); datalen = p[4] + (p[5]<<8); require(datalen + 10 <= inBufLen, exit); p += 6; err = PlatformUartRecv(p, datalen+2, 1000); require_noerr(err, exit); err = check_sum(inBuf, datalen + 10); require_noerr(err, exit); return datalen + 10; } exit: return -1; }
int download_data( void *datap, unsigned long start, unsigned long length ) { static unsigned char download_data_string[] = "$DD "; unsigned long check; unsigned long word_length = length / data_word_size; if ( length > 0 ) { download_data_string[3] = start >> 8; download_data_string[4] = start; download_data_string[5] = word_length >> 8; download_data_string[6] = word_length; check = check_sum( datap, length, data_word_size ); write_drain( ttyfd, download_data_string, sizeof(download_data_string) - 1 ); write_drain( ttyfd, datap, length ); if (received( 2 ) != data_word_size && check != data_word( buffer )) { fprintf( stderr, "Data download error: %x/%x\n", check, data_word( buffer ) ); length = 0; } }
int FileSaverUnit::save(const unsigned char *buffer) { PacketHeader *header = (PacketHeader *)buffer; if (header->block_offset != receive_block + 1) { // 说明读取网卡时丢包 return -1; } if (header->check_sum != check_sum(buffer + sizeof(PacketHeader), header->data_length)) { // 校验包失败 WriteLog("接收到的包校验失败"); return -1; } memcpy(this->buffer + this->receive_length, buffer + sizeof(PacketHeader), header->data_length); this->receive_block++; this->receive_length += header->data_length; // 接收文件完毕后保存文件 if (this->receive_block == block_num) { std::string new_filename; new_filename = filename; new_filename += ".recv"; // 创建目录 std::string path = new_filename.substr(0, new_filename.find_last_of("/")); CheckFolder(path.c_str()); FILE *file = fopen(new_filename.c_str(), "wb+"); fwrite(this->buffer, this->file_length, 1, file); fclose(file); return 2; } return 1; }
int main(int argc, char **argv) { int i,j, k, iptr; double **mat_a, **mat_b, **mat_c, **mat_d; double x, scal; double dtime; mat_a = (double **) malloc(MATSIZE * sizeof(double *)); mat_b = (double **) malloc(MATSIZE * sizeof(double *)); mat_c = (double **) malloc(MATSIZE * sizeof(double *)); mat_d = (double **) malloc(MATSIZE * sizeof(double *)); mat_a[0] = (double *) malloc(MATSIZE * MATSIZE * sizeof(double)); mat_b[0] = (double *) malloc(MATSIZE * MATSIZE * sizeof(double)); mat_c[0] = (double *) malloc(MATSIZE * MATSIZE * sizeof(double)); mat_d[0] = (double *) malloc(MATSIZE * MATSIZE * sizeof(double)); for (i = 0; i < MATSIZE; i++) { mat_a[i] = mat_a[0] + i*MATSIZE; mat_b[i] = mat_b[0] + i*MATSIZE; mat_c[i] = mat_c[0] + i*MATSIZE; mat_d[i] = mat_d[0] + i*MATSIZE; } /*--------------------------------------------------------------------------*/ printf("\n ----- Exercise 1 ------\n\n"); /* The code below generates three matrices. Try to think of a way in which this can be made parallel in any way. Make sure that the printed output x is correct in your parallel version */ dtime = gettime(); x = 0.35; for (j = 0; j < MATSIZE; j++) { for (i = 0; i < MATSIZE; i++) { x = 1 - sin(x); mat_a[i][j] = x; } } x = 0.68; for (j = 0; j < MATSIZE; j++) { for (i = 0; i < MATSIZE; i++) { x = 1 - sin(x); mat_b[i][j] = x; } } x = 0.24; for (j = 0; j < MATSIZE; j++) { for (i = 0; i < MATSIZE; i++) { x = 1 - sin(x); mat_c[i][j] = x; } } dtime = gettime() - dtime; printf("The sum of the matrices evaluates to:\n" "Matrix A:%12.4f\t Matrix B:%12.4f\t Matrix C:%12.4f \n", check_sum(mat_a), check_sum(mat_b), check_sum(mat_c)); printf("Time for the exercise: %9.5f seconds\n", dtime); printf("\n ----- End of exercise 1 ------\n\n"); /*--------------------------------------------------------------------------*/ #if (EXAMPLE > 1 && EXAMPLE < 3 || SERIAL) printf("\n ----- Exercise 2 ------\n\n"); /* This code makes a simple attempt at computing a matrix multiply. Try to parallelize it without changing the results (more than negligible) In this exercise parallelize the outer-most loop */ dtime = gettime(); for (i = 0; i < MATSIZE; i++) { for (j = 0; j < MATSIZE; j++) { scal = 0.0; for (k = 0; k < MATSIZE; k++) { scal += mat_a[i][k] * mat_b[k][j]; } mat_d[i][j] = scal; } } dtime = gettime() - dtime; printf("The sum of matrix D evaluates to: %12.4f\n", check_sum(mat_d)); printf("The value of scal is: %f\n", scal); printf("Time for the exercise: %9.5f seconds\n", dtime); printf("\n ----- End of exercise 2 ------\n\n"); #endif /*--------------------------------------------------------------------------*/ #if (EXAMPLE > 2 || SERIAL) printf("\n ----- Exercise 3 ------\n\n"); /* This code makes a simple attempt at computing a matrix multiply. Try to parallelize it without changing the results (more than negligible) In this exercise parallelize the second outer-most loop */ dtime = gettime(); for (i = 0; i < MATSIZE; i++) { for (j = 0; j < MATSIZE; j++) { scal = 0.0; for (k = 0; k < MATSIZE; k++) { scal += mat_a[i][k] * mat_b[k][j]; } mat_d[i][j] = scal; } } dtime = gettime() - dtime; printf("The sum of matrix D evaluates to: %12.4f\n", check_sum(mat_d)); printf("The value of scal is: %f\n", scal); printf("Time for the exercise: %9.5f seconds\n", dtime); printf("\n ----- End of exercise 3 ------\n\n"); #endif /*--------------------------------------------------------------------------*/ free(mat_d[0]); free(mat_c[0]); free(mat_b[0]); free(mat_a[0]); free(mat_d); free(mat_c); free(mat_b); free(mat_a); return 0; }
/******************************************************************************* *函数名称: load_Boot1_from_nand *函数原型:int32 load_Boot1_from_nand( void ) *函数功能: 将一份好的Boot1从nand flash中载入到SRAM中。 *入口参数: void *返 回 值: OK 载入并校验成功 * ERROR 载入并校验失败 *备 注: *******************************************************************************/ __s32 load_Boot1_from_nand( void ) { __u32 i; __s32 status; __u32 length; __u32 read_blks; struct spare_boot_head_t *bfh = (struct spare_boot_head_t *) CONFIG_SYS_TEXT_BASE;; if(NF_open( ) == NF_ERROR) // 打开nand flash { printf("fail in opening nand flash\n"); return -1; } //printf("Succeed in opening nand flash.\n"); //printf("block from %d to %d\n", BOOT1_START_BLK_NUM, BOOT1_LAST_BLK_NUM); for( i = BOOT1_START_BLK_NUM; i <= BOOT1_LAST_BLK_NUM; i++ ) { if( NF_read_status( i ) == NF_BAD_BLOCK ) // 如果当前块是坏块,则进入下一块 { printf("nand block %d is bad\n", i); continue; } /* 载入当前块最前面512字节的数据到SRAM中,目的是获取文件头 */ if( NF_read( i << ( NF_BLK_SZ_WIDTH - NF_SCT_SZ_WIDTH ), (void *)CONFIG_SYS_TEXT_BASE, 1 ) == NF_OVERTIME_ERR ) { printf("the first data is error\n"); continue; } //printf("Succeed in reading Boot1 file head.\n"); /* 察看是否是文件头 */ if( check_magic( (__u32 *)CONFIG_SYS_TEXT_BASE, UBOOT_MAGIC ) != 0 ) { printf("ERROR! block %u doesn't store head of Boot1 copy.\n", i ); continue; } length = bfh->boot_head.length; //printf("The size of Boot1 is %x.\n", length ); //printf("The align size of Boot1 is %x.\n", NF_SECTOR_SIZE ); if( ( length & ( NF_SECTOR_SIZE - 1 ) ) != 0 ) // length必须是NF_SECTOR_SIZE对齐的 { printf("the boot1 is not aligned by %x\n", bfh->boot_head.align_size); continue; } //printf("The size of Boot1 is %x.\n", length ); if( 1==load_uboot_in_one_block_judge(length) ) { /* 从一个块中载入Boot1的备份 */ status = load_and_check_in_one_blk( i, (void *)CONFIG_SYS_TEXT_BASE, length, NF_BLOCK_SIZE ); if( status == ADV_NF_OVERTIME_ERR ) // 块数不足 continue; else if( status == ADV_NF_OK ) { //printf("Check is correct.\n"); bfh->boot_data.storage_type = 0; NF_close( ); // 关闭nand flash return 0; } } else { /* 从多个块中载入一份Boot1的备份 */ status = load_in_many_blks( i, BOOT1_LAST_BLK_NUM, (void*)CONFIG_SYS_TEXT_BASE, length, NF_BLOCK_SIZE, &read_blks ); if( status == ADV_NF_LACK_BLKS ) // 块数不足 { printf("ADV_NF_LACK_BLKS\n"); NF_close( ); // 关闭nand flash return -1; } else if( status == ADV_NF_OVERTIME_ERR ) { printf("mult block ADV_NF_OVERTIME_ERR\n"); continue; } if( check_sum( (__u32 *)CONFIG_SYS_TEXT_BASE, length ) == 0 ) { printf("The file stored in start block %u is perfect.\n", i ); bfh->boot_data.storage_type = 0; NF_close( ); // 关闭nand flash return 0; } } } printf("Can't find a good Boot1 copy in nand.\n"); NF_close( ); // 关闭nand flash printf("Ready to quit \"load_Boot1_from_nand\".\n"); return -1; }