1

I am trying to count to 1000 with single core and dual core with esp32 but the output(run time) dose not make sense, the time which takes a single core should be doubled compared to dual core in theory right?

Here is the code

TaskHandle_t Task1;
TaskHandle_t Task2;
SemaphoreHandle_t baton;

int i = 0;
long l;

void setup() {

  Serial.begin(115200);
  pinMode(2, OUTPUT);
  pinMode(4, OUTPUT);

  baton = xSemaphoreCreateMutex();
  l = millis();
  xTaskCreatePinnedToCore(
    Task1code, /* Function to implement the task */
    "Task1", /* Name of the task */
    10000,  /* Stack size in words */
    NULL,  /* Task input parameter */
    1,  /* Priority of the task */
    &Task1,  /* Task handle. */
    0); /* Core where the task should run */

//single core or dual core execution

//  xTaskCreatePinnedToCore(
//    Task2code, /* Function to implement the task */
//    "Task2", /* Name of the task */
//    10000,  /* Stack size in words */
//    NULL,  /* Task input parameter */
//    1,  /* Priority of the task */
//    &Task2,  /* Task handle. */
//    1); /* Core where the task should run */

}
void Task1code( void * parameter) {
  while (true) {
    xSemaphoreTake(baton, portMAX_DELAY);
    xSemaphoreGive(baton);
    delay(1);// I don't understand in here without this command output result is worse
    i++;
    if (i == 1000) {
      l = millis()-1;
      Serial.print("Done ");
      Serial.print(xPortGetCoreID());
      Serial.print(" ");
      Serial.println(l);
      
    }if(i>1000){
      vTaskDelete(Task1);
    }
  }
}
void Task2code( void * parameter) {
  while (true) {
    xSemaphoreTake(baton, portMAX_DELAY);
    xSemaphoreGive(baton);
    delay(1);// I don't understand in here without this command output result is worse
    i++;
    if (i == 1000) {
      l = millis()-1;
      Serial.print("Done ");
      Serial.print(xPortGetCoreID());
      Serial.print(" ");
      Serial.println(l);
    }if(i>1000){
      vTaskDelete(Task2);
    }
  }
}
void loop() {

}

The output I get for runtime

Single core :- 1033 ms
Dual core :- 701 ms

Without delay(1); command the result is even more obvious

Single core :- 39 ms
Dual core :- 40 ms

How can I make my algorithm show that single core takes twice time as dual core execution time I wan't both cores to work parallel not one after the other.

1
  • Only one task can be updating i at once. Without delay the act of doing the counting is negligible. It's the semaphore that is giving you any results at all.
    – Majenko
    Aug 9 '20 at 15:01
2

For multiple cores to provide any advantage, they must be able to work independently. If they require shared access to a resource - i in your experiment - only one core can actually be contributing to the job at any one time. But because there are multiple, they have the additional bookkeeping to negotiate access to the shared resource. Thus the total job takes longer than it would take one core with complete access to the resource and no overhead to manage access, to do the job by itself.

1
  • Thanks this helped me
    – Avon97
    Sep 6 at 13:09
2

Your must use the semaphore to protect the read/write access to i (Only one task is allowed to modify the variable at a time)

xSemaphoreTake(baton, portMAX_DELAY);
i = i + 1;
xSemaphoreGive(baton);

Additionally you should combine your if clauses. If both tasks increment iit could happen, that it will never has the value 1000for the second task.

3
  • But it did not speed up the execution time? Here is the updated code as you asked hastebin.com/ajileyiqud.cpp
    – Avon97
    Aug 11 '20 at 11:59
  • Right now (without the delay) you couldn't speed up the execution time with a second task. Only one task is allowed to access iat a time and so the other task has to wait and could do nothing. If you gives the task something to do (the delay) you should se the difference.
    – theSealion
    Aug 11 '20 at 12:45
  • I don't understand how to get advantage of the dual cores it has?
    – Avon97
    Aug 11 '20 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.