This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

关于C6748计算速度问题

按手册上的介绍,C6748峰值性能是3648MIPS和2746MFLOPS, 我现在用一段测试代码进行加法运算,代码如下:

#include <stdio.h>
#include <c6x.h>
#include <c67fastMath.h>

int main(void) {
unsigned long long T_start_lib, T_end_lib;
unsigned long long cnt1 = 0;
long long i = 0;

printf("game start\n");

TSCH = 0;
TSCL = 0;

T_start_lib = _itoll(TSCH, TSCL);

for(i = 0; i < 1000000; i++)
{
}

T_end_lib = _itoll(TSCH, TSCL);

cnt1 = T_end_lib - T_start_lib;

printf("T_end_lib = %ld, T_start_lib = %ld\n", T_end_lib, T_start_lib);
printf("cnt1 = %ld\n", cnt1);

return 0;

}

在未进行任何优化的情况下,运行结果是需要110000088cycle,也就是110000088/456M约241.2ms

开了-03优化后,运行结果是需要1000012cycle,也就是1000012/456M约2.19ms

但是如果按理论峰值来计算的话,应该是1000000次/3648MIPS,也就是约0.274ms

也就是说现在的计算能力只能到峰值能力的1/8左右,那么请问我需要进行什么样的优化来提高这个运行速度呢?可以提供相关的demo吗?

另外,cmd文件未做修改,optimization设置如图

  • 另外还有个问题,在这个测试的基础上添加了一个查表法进行atan运算,代码如下:

    #include <stdio.h>
    #include <c6x.h>
    #include <c67fastMath.h>
    #define PI 3.14159265354

    static const int tb_atan[1000] =
    {
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
    21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
    41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
    61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
    81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100,
    101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119,
    120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139,
    140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
    160, 161, 162, 163, 164, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178,
    179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 195, 196, 197,
    198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217,
    218, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236,
    236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 252, 253, 254,
    255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 267, 268, 269, 270, 271, 272, 273,
    274, 275, 276, 277, 278, 279, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 291,
    292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 302, 303, 304, 305, 306, 307, 308, 309, 310,
    311, 312, 312, 313, 314, 315, 316, 317, 318, 319, 320, 321, 321, 322, 323, 324, 325, 326, 327, 328,
    329, 330, 330, 331, 332, 333, 334, 335, 336, 337, 338, 338, 339, 340, 341, 342, 343, 344, 345, 346,
    346, 347, 348, 349, 350, 351, 352, 353, 354, 354, 355, 356, 357, 358, 359, 360, 361, 361, 362, 363,
    364, 365, 366, 367, 368, 368, 369, 370, 371, 372, 373, 374, 374, 375, 376, 377, 378, 379, 380, 381,
    381, 382, 383, 384, 385, 386, 387, 387, 388, 389, 390, 391, 392, 393, 393, 394, 395, 396, 397, 398,
    398, 399, 400, 401, 402, 403, 404, 404, 405, 406, 407, 408, 409, 409, 410, 411, 412, 413, 414, 415,
    415, 416, 417, 418, 419, 420, 420, 421, 422, 423, 424, 425, 425, 426, 427, 428, 429, 429, 430, 431,
    432, 433, 434, 434, 435, 436, 437, 438, 439, 439, 440, 441, 442, 443, 443, 444, 445, 446, 447, 448,
    448, 449, 450, 451, 452, 452, 453, 454, 455, 456, 456, 457, 458, 459, 460, 460, 461, 462, 463, 464,
    464, 465, 466, 467, 468, 468, 469, 470, 471, 472, 472, 473, 474, 475, 476, 476, 477, 478, 479, 480,
    480, 481, 482, 483, 483, 484, 485, 486, 487, 487, 488, 489, 490, 490, 491, 492, 493, 494, 494, 495,
    496, 497, 497, 498, 499, 500, 501, 501, 502, 503, 504, 504, 505, 506, 507, 507, 508, 509, 510, 510,
    511, 512, 513, 514, 514, 515, 516, 517, 517, 518, 519, 520, 520, 521, 522, 523, 523, 524, 525, 526,
    526, 527, 528, 529, 529, 530, 531, 532, 532, 533, 534, 535, 535, 536, 537, 537, 538, 539, 540, 540,
    541, 542, 543, 543, 544, 545, 546, 546, 547, 548, 548, 549, 550, 551, 551, 552, 553, 554, 554, 555,
    556, 556, 557, 558, 559, 559, 560, 561, 561, 562, 563, 564, 564, 565, 566, 566, 567, 568, 569, 569,
    570, 571, 571, 572, 573, 574, 574, 575, 576, 576, 577, 578, 578, 579, 580, 581, 581, 582, 583, 583,
    584, 585, 585, 586, 587, 588, 588, 589, 590, 590, 591, 592, 592, 593, 594, 594, 595, 596, 596, 597,
    598, 599, 599, 600, 601, 601, 602, 603, 603, 604, 605, 605, 606, 607, 607, 608, 609, 609, 610, 611,
    611, 612, 613, 613, 614, 615, 615, 616, 617, 617, 618, 619, 619, 620, 621, 621, 622, 623, 623, 624,
    625, 625, 626, 627, 627, 628, 629, 629, 630, 631, 631, 632, 633, 633, 634, 634, 635, 636, 636, 637,
    638, 638, 639, 640, 640, 641, 642, 642, 643, 644, 644, 645, 645, 646, 647, 647, 648, 649, 649, 650,
    651, 651, 652, 652, 653, 654, 654, 655, 656, 656, 657, 657, 658, 659, 659, 660, 661, 661, 662, 662,
    663, 664, 664, 665, 666, 666, 667, 667, 668, 669, 669, 670, 670, 671, 672, 672, 673, 674, 674, 675,
    675, 676, 677, 677, 678, 678, 679, 680, 680, 681, 681, 682, 683, 683, 684, 684, 685, 686, 686, 687,
    687, 688, 689, 689, 690, 690, 691, 692, 692, 693, 693, 694, 695, 695, 696, 696, 697, 697, 698, 699,
    699, 700, 700, 701, 702, 702, 703, 703, 704, 704, 705, 706, 706, 707, 707, 708, 709, 709, 710, 710,
    711, 711, 712, 713, 713, 714, 714, 715, 715, 716, 717, 717, 718, 718, 719, 719, 720, 721, 721, 722,
    722, 723, 723, 724, 724, 725, 726, 726, 727, 727, 728, 728, 729, 729, 730, 731, 731, 732, 732, 733,
    733, 734, 734, 735, 736, 736, 737, 737, 738, 738, 739, 739, 740, 740, 741, 742, 742, 743, 743, 744,
    744, 745, 745, 746, 746, 747, 748, 748, 749, 749, 750, 750, 751, 751, 752, 752, 753, 753, 754, 754,
    755, 756, 756, 757, 757, 758, 758, 759, 759, 760, 760, 761, 761, 762, 762, 763, 763, 764, 764, 765,
    766, 766, 767, 767, 768, 768, 769, 769, 770, 770, 771, 771, 772, 772, 773, 773, 774, 774, 775, 775,
    776, 776, 777, 777, 778, 778, 779, 779, 780, 780, 781, 781, 782, 782, 783, 783, 784, 784, 785, 785
    };

    float tb_ph(float x)
    {
    x = x;
    int index;
    float ph;
    if (x == 0)
    {
    ph = 0;
    }
    if (x == 1)
    {
    ph = PI / 4;
    }
    if (x == -1)
    {
    ph = -PI / 4;
    }
    if (x>0 && x<1)
    {
    index = (unsigned int)(x*1000.0 + 0.5);
    ph = tb_atan[index];
    ph = (float)((float)(ph) / 1000.0);
    }
    if (x>-1 && x<0)
    {
    x = -x;
    index = (int)(x*1000.0 + 0.5);
    ph = tb_atan[index];
    ph = (float)((float)(ph) / 1000.0);
    ph = -ph;
    }
    if (x>1)
    {
    x = 1.0 / x;
    index = (int)(x*1000.0 + 0.5);
    ph = tb_atan[index];
    ph = (float)((float)(ph) / 1000.0);
    ph = (PI / 2 - ph);
    }
    if (x<-1)
    {
    x = -1.0/x;
    index = (int)(x*1000.0 + 0.5);
    ph = tb_atan[index];
    ph = -ph;
    ph = (float)((float)(ph) / 1000.0);
    ph = (-PI / 2 - ph);
    }
    return ph;
    }


    int main(void) {
    unsigned long long T_start_lib, T_end_lib, T_start, T_end;
    unsigned long long cnt1 = 0;
    float temp;
    long long i = 0;

    printf("game start\n");

    TSCH = 0;
    TSCL = 0;

    T_start = _itoll(TSCH, TSCL);

    for(i = 0; i < 1000000; i++)
    {
    temp = tb_ph(PI*(float)i);
    }
    T_end = _itoll(TSCH, TSCL);

    printf("\n");
    cnt1 = T_end - T_start;

    printf("T_end = %ld, T_start = %ld\n", T_end, T_start);

    printf("cnt1 = %ld\n", cnt1);


    return 0;
    }

    测试的结果是需要1000012cycle,与只做i++运算需要的时间相同,请问这合理吗? 还是我哪边测试有问题啊?

  • c674x是VLIW架构,这个MIPS/MFLOPS值是指当8条指令在8个单元同时并行执行,没有资源冲突情况下的理论值,实际应用中很难保证资源不冲突(如memory stall的发生)。所以您说的是个理论值,在实际板子上是达不到的。

    一般通过开优化选项,时间要求高的代码放在片上执行。如果代码/数据放在片外,可以用edma搬移,并开启cache。
  • 您可以去分析一下tb_ph函数和i++的汇编指令。或者在tb_ph函数里添加i++指令,看执行时间是否有变化。
  • 我觉得这个不合理啊 不管tb_ph函数里面怎么运行,还增加了一个PI*i的乘法,但是运行时间却是一样的,不能理解
  • 请问要如何实现8条指令或者少一点4条指令在8个单元上同时并行执行呢? 我现在尝试了开优化选项 开cache这些操作 感觉运行速度并没有很大的提升啊