Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Is QT Automatically optimizing my loop to run on multiple cores?
Forum Updated to NodeBB v4.3 + New Features

Is QT Automatically optimizing my loop to run on multiple cores?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Curtwagner1984
    wrote on 12 May 2017, 20:28 last edited by
    #1

    I apologize in advance if this question seems trivial.

    I have a SQL database that consists of Movies,Actors,Tags and Pictures.
    A Movie and Picture tables have 'PathToFile' columns which contains the path of the movie or the picture on disk.
    Tags, and Actors table, have 'name' columns which contain the tag or actor name.

    I have a class called Tagger whos job is to go over all pictures and movies paths and see if the path contains an actor or tag name, if it does it would add the relation to the database.

    I'm testing this with over 70K pictures in the database so the process takes some time, however when I open Task Manager (I'm using windows 10.) While Tagger is doing it's work, I see the workload is evenly spread across all 4 of the CPU cores.
    alt text

    I didn't do any multi-threading optimization and am curious as to what is going on.

    Here is the code for the loop function :

    void Tagger::tag(QList<QMap<QString, QVariant> > thingsToTag,
                     QList<QMap<QString, QVariant> > preparedTaggingElemets,
                     QList<QStringList> &actors, QList<QStringList> &tags, QList<QStringList> &websites, QString thingToTagType)
    {
        for (int i = 0 ; i < thingsToTag.size() ; i++)
        {
            QMap<QString, QVariant> currentThingToTag =  thingsToTag.at(i);
            QString thingToTagName = this->prepareForComparison(currentThingToTag["path_to_file"].toString());
    
            qDebug() << "Checking " << i << " Out of " << thingsToTag.size() << " " + thingToTagType +  " " << thingToTagName << " for tags...";
            for (int j = 0 ; j < preparedTaggingElemets.size() ; j++)
            {
                QMap<QString, QVariant> currentTaggingElement =  preparedTaggingElemets.at(j);
                QString nameToCheck = currentTaggingElement["name"].toString();
    
    
                if (thingToTagName.contains(nameToCheck)){
    
                    qDebug() << "Found " << nameToCheck << "in" << thingToTagName;
    
                    if (currentTaggingElement["table_name"] == "actor")
                    {
                        QStringList temp;
                        temp.append(currentThingToTag["id"].toString());
                        temp.append(currentTaggingElement["id"].toString());
                        actors.append(temp);
    
                    }else if (currentTaggingElement["table_name"] == "tag")
                    {
                        QStringList temp;
                        temp.append(currentThingToTag["id"].toString());
                        temp.append(currentTaggingElement["id"].toString());
                        tags.append(temp);
    
                    }else if (currentTaggingElement["table_name"] == "website")
                    {
                        QStringList temp;
                        temp.append(currentThingToTag["id"].toString());
                        temp.append(currentTaggingElement["id"].toString());
                        websites.append(temp);
    
    
                    }else if (currentTaggingElement["table_name"] == "actorAlias")
                    {
                        QStringList temp;
                        temp.append(currentThingToTag["id"].toString());
                        temp.append(currentTaggingElement["alias_of"].toString());
                        actors.append(temp);
    
    
                    }else if (currentTaggingElement["table_name"] == "tagAlias")
                    {
                        QStringList temp;
                        temp.append(currentThingToTag["id"].toString());
                        temp.append(currentTaggingElement["alias_of"].toString());
                        tags.append(temp);
    
                    }else if (currentTaggingElement["table_name"] == "websiteAlias")
                    {
                        QStringList temp;
                        temp.append(currentThingToTag["id"].toString());
                        temp.append(currentTaggingElement["alias_of"].toString());
                        websites.append(temp);
                    }
                }
                thingToTagName = thingToTagName.replace(nameToCheck,"");
    
            }
        }
    
    }
    
    K 1 Reply Last reply 12 May 2017, 21:34
    0
    • C Curtwagner1984
      12 May 2017, 20:28

      I apologize in advance if this question seems trivial.

      I have a SQL database that consists of Movies,Actors,Tags and Pictures.
      A Movie and Picture tables have 'PathToFile' columns which contains the path of the movie or the picture on disk.
      Tags, and Actors table, have 'name' columns which contain the tag or actor name.

      I have a class called Tagger whos job is to go over all pictures and movies paths and see if the path contains an actor or tag name, if it does it would add the relation to the database.

      I'm testing this with over 70K pictures in the database so the process takes some time, however when I open Task Manager (I'm using windows 10.) While Tagger is doing it's work, I see the workload is evenly spread across all 4 of the CPU cores.
      alt text

      I didn't do any multi-threading optimization and am curious as to what is going on.

      Here is the code for the loop function :

      void Tagger::tag(QList<QMap<QString, QVariant> > thingsToTag,
                       QList<QMap<QString, QVariant> > preparedTaggingElemets,
                       QList<QStringList> &actors, QList<QStringList> &tags, QList<QStringList> &websites, QString thingToTagType)
      {
          for (int i = 0 ; i < thingsToTag.size() ; i++)
          {
              QMap<QString, QVariant> currentThingToTag =  thingsToTag.at(i);
              QString thingToTagName = this->prepareForComparison(currentThingToTag["path_to_file"].toString());
      
              qDebug() << "Checking " << i << " Out of " << thingsToTag.size() << " " + thingToTagType +  " " << thingToTagName << " for tags...";
              for (int j = 0 ; j < preparedTaggingElemets.size() ; j++)
              {
                  QMap<QString, QVariant> currentTaggingElement =  preparedTaggingElemets.at(j);
                  QString nameToCheck = currentTaggingElement["name"].toString();
      
      
                  if (thingToTagName.contains(nameToCheck)){
      
                      qDebug() << "Found " << nameToCheck << "in" << thingToTagName;
      
                      if (currentTaggingElement["table_name"] == "actor")
                      {
                          QStringList temp;
                          temp.append(currentThingToTag["id"].toString());
                          temp.append(currentTaggingElement["id"].toString());
                          actors.append(temp);
      
                      }else if (currentTaggingElement["table_name"] == "tag")
                      {
                          QStringList temp;
                          temp.append(currentThingToTag["id"].toString());
                          temp.append(currentTaggingElement["id"].toString());
                          tags.append(temp);
      
                      }else if (currentTaggingElement["table_name"] == "website")
                      {
                          QStringList temp;
                          temp.append(currentThingToTag["id"].toString());
                          temp.append(currentTaggingElement["id"].toString());
                          websites.append(temp);
      
      
                      }else if (currentTaggingElement["table_name"] == "actorAlias")
                      {
                          QStringList temp;
                          temp.append(currentThingToTag["id"].toString());
                          temp.append(currentTaggingElement["alias_of"].toString());
                          actors.append(temp);
      
      
                      }else if (currentTaggingElement["table_name"] == "tagAlias")
                      {
                          QStringList temp;
                          temp.append(currentThingToTag["id"].toString());
                          temp.append(currentTaggingElement["alias_of"].toString());
                          tags.append(temp);
      
                      }else if (currentTaggingElement["table_name"] == "websiteAlias")
                      {
                          QStringList temp;
                          temp.append(currentThingToTag["id"].toString());
                          temp.append(currentTaggingElement["alias_of"].toString());
                          websites.append(temp);
                      }
                  }
                  thingToTagName = thingToTagName.replace(nameToCheck,"");
      
              }
          }
      
      }
      
      K Offline
      K Offline
      koahnig
      wrote on 12 May 2017, 21:34 last edited by
      #2

      @Curtwagner1984

      Mostly likely you are seeing this phenomenon
      You should see for your application on the details tab. Most likely it will consume up to 25%. When you have done something to use multi-threading it may consume on optimum almost a 100%.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      3
      • C Offline
        C Offline
        Curtwagner1984
        wrote on 12 May 2017, 21:54 last edited by
        #3

        @koahnig Thank you! Exactly what I wanted to know.

        E 1 Reply Last reply 15 May 2017, 08:42
        0
        • C Curtwagner1984
          12 May 2017, 21:54

          @koahnig Thank you! Exactly what I wanted to know.

          E Offline
          E Offline
          Eeli K
          wrote on 15 May 2017, 08:42 last edited by
          #4

          @Curtwagner1984 Just out of curiousity, are your planning to optimize your code for multicore parallelism?

          C 1 Reply Last reply 15 May 2017, 09:33
          0
          • E Eeli K
            15 May 2017, 08:42

            @Curtwagner1984 Just out of curiousity, are your planning to optimize your code for multicore parallelism?

            C Offline
            C Offline
            Curtwagner1984
            wrote on 15 May 2017, 09:33 last edited by
            #5

            @Eeli-K If I'll have time yes. This particular function runs fast enough in a single thread, if I'll won't have other pressing concerns I'll optimize it.

            E 1 Reply Last reply 15 May 2017, 11:16
            0
            • C Curtwagner1984
              15 May 2017, 09:33

              @Eeli-K If I'll have time yes. This particular function runs fast enough in a single thread, if I'll won't have other pressing concerns I'll optimize it.

              E Offline
              E Offline
              Eeli K
              wrote on 15 May 2017, 11:16 last edited by
              #6

              @Curtwagner1984 I thought this might be an opportunity to use the former experimental, nowadays standardized (C++17) parallel STL algorithms. I don't know how to use them but it surely looks interesting. You don't have to create threads, just use the algorithms or even loops like for_each and they take care of parallelizing themselves. Your data and your own algorithms must be of course designed to be thread-safe. Maybe some C++ guru can give an opinion.

              C 1 Reply Last reply 16 May 2017, 09:05
              0
              • E Eeli K
                15 May 2017, 11:16

                @Curtwagner1984 I thought this might be an opportunity to use the former experimental, nowadays standardized (C++17) parallel STL algorithms. I don't know how to use them but it surely looks interesting. You don't have to create threads, just use the algorithms or even loops like for_each and they take care of parallelizing themselves. Your data and your own algorithms must be of course designed to be thread-safe. Maybe some C++ guru can give an opinion.

                C Offline
                C Offline
                Curtwagner1984
                wrote on 16 May 2017, 09:05 last edited by
                #7

                @Eeli-K Sounds interesting

                1 Reply Last reply
                0

                7/7

                16 May 2017, 09:05

                • Login

                • Login or register to search.
                7 out of 7
                • First post
                  7/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved