main.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include <stdio.h>
  2. #include "connectionManager.hpp"
  3. ConnectionManager connectionManager;
  4. Module mod = Module::conv2D_5x5_Module;
  5. unsigned int jobsPerWorker = 100;
  6. size_t s=0, f=0, r=0;
  7. std::mutex statsLk;
  8. void work() {
  9. auto worker = connectionManager.createWorker(mod, jobsPerWorker);
  10. worker->setJobTimeout(milliseconds(1000));
  11. worker->setRetryCount(10);
  12. worker->setDoneCallback([worker](){
  13. auto jobs = worker->getJobList();
  14. std::unique_lock<std::mutex> lk(statsLk);
  15. for(size_t i=0; i<jobs->getJobCount(); i++) {
  16. auto job = jobs->getJob(i);
  17. if(job->getState() == JobState::finished) {
  18. s++;
  19. } else if(job->getState() == JobState::failed) {
  20. f++;
  21. } else {
  22. printf("job %08X: invalid state %d\n", job->getJobId(), (int)job->getState());
  23. }
  24. r += job->getSendCounter() - 1;
  25. }
  26. });
  27. {
  28. auto jobs = worker->getJobList();
  29. for(size_t i=0; i<jobs->getJobCount(); i++) {
  30. auto job = jobs->getJob(i);
  31. static int num=0;
  32. job->setPayload(0, num);
  33. job->setPayload(1, num);
  34. job->setPayload(2, num);
  35. job->setPayload(3, num++);
  36. job->setReady();
  37. }
  38. }
  39. worker->startAsync();
  40. }
  41. int main(int argc, char *argv[])
  42. {
  43. puts("This is a shared library test...");
  44. unsigned int numFPGA = 3;
  45. unsigned int workNum = 100;
  46. unsigned int workerCount = 1;
  47. if(argc > 1)
  48. numFPGA = atoi(argv[1]);
  49. if(numFPGA >= 1)
  50. connectionManager.addFPGA("192.168.1.33", 1234);
  51. if(numFPGA >= 2)
  52. connectionManager.addFPGA("192.168.1.34", 1234);
  53. if(numFPGA >= 3)
  54. connectionManager.addFPGA("192.168.1.35", 1234);
  55. connectionManager.setSendDelay(microseconds(50));
  56. connectionManager.start();
  57. if(argc > 2)
  58. workNum = atoi(argv[2]);
  59. if(argc > 3)
  60. workerCount = atoi(argv[3]);
  61. if(argc > 4)
  62. jobsPerWorker = atoi(argv[4]);
  63. printf("arguments: <numFPGA = %u> <workNum = %u> <workerCount = %u> <jobsPerWorker = %u>\n", numFPGA, workNum, workerCount, jobsPerWorker);
  64. while(workNum > 0 || connectionManager.getWorkerCount() > 0) {
  65. std::this_thread::sleep_for(microseconds(1000));
  66. connectionManager.removeFinishedWorkers();
  67. while(workNum > 0 && connectionManager.getWorkerCount() < workerCount) {
  68. workNum--;
  69. work();
  70. std::unique_lock<std::mutex> lk(statsLk);
  71. printf("work: %2d worker: %2lu failed: %12lu, successful: %12lu, retries: %12lu\n", workNum, connectionManager.getWorkerCount(), f, s, r);
  72. }
  73. }
  74. std::unique_lock<std::mutex> lk(statsLk);
  75. printf("work: %2d worker: %2lu failed: %12lu, successful: %12lu, retries: %12lu\n", workNum, connectionManager.getWorkerCount(), f, s, r);
  76. return 0;
  77. }